# sorting decimal numbers correctly

• 01-26-2013, 01:37 PM
jmcall10
sorting decimal numbers correctly
Ok I have a section of code that sorts the names it is given alphabetically.

However the code doesnt handle decimals the way I would want.

It orders the name in the following manner (Obv I would rather it incremented numerically):

It would order it:
APPLE - 1.0051
APPLE - 1.1071
APPLE - 11.1592
APPLE - 12.0692
APPLE - 12.1717
APPLE - 2.0186 << this should be after "APPLE - 1.1071" obviously
APPLE - 21.1407
APPLE - 22.089
APPLE - 23.069
BANANA - 1.0051
BANANA - 1.1071
BANANA - 11.1592
BANANA - 12.0692
BANANA - 12.1717
BANANA - 2.0186 << this should be after "BANANA - 1.1071" obviously
BANANA - 21.1407
BANANA - 22.089
BANANA - 23.069

Here is the code I am using. I do not fully understand the code as it was a snippet I have been using.

Code:

```function(a, b){                         var nameA=a.myname.toLowerCase(), nameB=b.myname.toLowerCase()                         if (nameA < nameB) //sort string ascending                           return -1                         if (nameA > nameB)                           return 1                         return 0 //default return value (no sorting)                 }```
Regards,
jmcall10 :confused:
• 01-26-2013, 02:04 PM
iBeZi
You're only sorting by the name, you need to sort by the value as well.

Code:

```function(a, b){         var nameA=a.myname.toLowerCase(), nameB=b.myname.toLowerCase()         var valA=a.value, valB=b.value;         if (nameA < nameB || valA < valB) //sort string ascending           return -1         if (nameA > nameB || valA > valB)           return 1         return 0 //default return value (no sorting) }```
• 01-26-2013, 02:30 PM
jmcall10
Ok,

I have implemented your solution and it has now lost the initial Alphabetic sorting. I cannot deduce what method it is using to sort now :S
• 01-26-2013, 02:36 PM
iBeZi
What format are the names/values your sorting in? I'm assuming it's an array of objects like this

Code:

```var array = [   {       "myname":"APPLE",       "value":1.0051   },   {       "myname":"APPLE",       "value":1.1071   } ];```
And then you're using the sort function, passing through the function above as the parameter like so

Code:

```array.sort(   function(a, b){         var nameA=a.myname.toLowerCase(), nameB=b.myname.toLowerCase()         var valA=a.value, valB=b.value;         if (nameA < nameB || valA < valB) //sort string ascending           return -1         if (nameA > nameB || valA > valB)           return 1         return 0 //default return value (no sorting)   } );```
• 01-26-2013, 03:14 PM
jmcall10
Yes effectively that is what is happening.

However the value is within the myname variable. for example "Apple - 1.0051" is the full myname.
• 01-26-2013, 03:27 PM
iBeZi
This should do it

Code:

```function(a, b){         var splitA = a.myname.split("-");         var splitB = b.myname.split("-");         var nameA=splitA[0].toLowerCase(), nameB=splitB[0].toLowerCase()         var valA=parseFloat(splitA[1]), valB=parseFloat(splitB[1]);         if (nameA < nameB || valA < valB) //sort string ascending         return -1         if (nameA > nameB || valA > valB)         return 1         return 0 //default return value (no sorting) }```
• 01-26-2013, 03:31 PM
jmcall10
Ok, this code is now not sorting alphabetically at all. Is there perhaps and error in the code?

It should sort alphabetically and then numerically.

Thanks again for looking into this for me.
• 01-26-2013, 03:44 PM
jmcall10
Ok got it working from another forum using this code:
Code:

```function(a, b) {     var parts = {         a: a.myname.split(' - '),         b: b.myname.split(' - ')     };     if (parts.a[0] == parts.b[0]) // strings are the same         return parseFloat(parts.a[1]) - parseFloat(parts.b[1]); // sort by number     return parts.a[0] > parts.b[0] ? 1 : -1; // sort by string }```
• 01-26-2013, 04:25 PM
JMRKER
Glad you figured it out. Took me too long to replicate you data. Here is an alternative method...
Code:

```<div id="debugger"></div> <script type="text/javascript"> // Modified from: http://answers.yahoo.com/question/index?qid=20070718104153AA232co function addMarkerObj(arr, nam, num){ arr[arr.length] = {distance:num, name:nam}; } function sortNumberObj(a, b){ return a.distance - b.distance; }                    //no need for an array index with objects function sortAlphaObj(a, b){ return a.name.toLowerCase() > b.name.toLowerCase(); } //no need for an array index with objects function displayMarkersObj(arr){   var str = "";   for(var i = 0; i < arr.length; i++) {     str += "<br>distance: " + arr[i].distance + " name: " + arr[i].name;  // change display sequence as desired   }   return str; } var rmarkers = []; addMarkerObj(rmarkers, 'APPLE', 1.0051); addMarkerObj(rmarkers, 'APPLE', 1.1071); addMarkerObj(rmarkers, 'APPLE', 11.1592); addMarkerObj(rmarkers, 'APPLE', 12.0692); addMarkerObj(rmarkers, 'APPLE', 12.1717); addMarkerObj(rmarkers, 'APPLE', 2.0186); addMarkerObj(rmarkers, 'APPLE', 21.1407); addMarkerObj(rmarkers, 'APPLE', 22.089); addMarkerObj(rmarkers, 'APPLE', 23.069); addMarkerObj(rmarkers, 'BANANA', 1.0051); addMarkerObj(rmarkers, 'BANANA', 1.1071); addMarkerObj(rmarkers, 'BANANA', 11.1592); addMarkerObj(rmarkers, 'BANANA', 12.0692); addMarkerObj(rmarkers, 'BANANA', 12.1717); addMarkerObj(rmarkers, 'BANANA', 2.0186); addMarkerObj(rmarkers, 'BANANA', 21.1407); addMarkerObj(rmarkers, 'BANANA', 22.089); addMarkerObj(rmarkers, 'BANANA', 23.069); var str = ''; str = displayMarkersObj(rmarkers); document.getElementById('debugger').innerHTML = 'Un-sorted:'+str; rmarkers.sort(sortNumberObj); str = displayMarkersObj(rmarkers); document.getElementById('debugger').innerHTML += '<p>Distance sort:'+str; rmarkers.sort(sortAlphaObj); str = displayMarkersObj(rmarkers); document.getElementById('debugger').innerHTML += '<p>Name sort:'+str; </script>```

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.11136 seconds
• Memory Usage 2,379KB
• Queries Executed 11 (?)
Template Usage (20):
• (7)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates