Thread: sorting decimal numbers correctly

1. Registered User
Join Date
Oct 2004
Posts
11

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

2. Registered User
Join Date
Dec 2012
Posts
264
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)
}```

3. Registered User
Join Date
Oct 2004
Posts
11
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

4. Registered User
Join Date
Dec 2012
Posts
264
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)
}
);```

5. Registered User
Join Date
Oct 2004
Posts
11
Yes effectively that is what is happening.

However the value is within the myname variable. for example "Apple - 1.0051" is the full myname.

6. Registered User
Join Date
Dec 2012
Posts
264
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)
}```

7. Registered User
Join Date
Oct 2004
Posts
11
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.

8. Registered User
Join Date
Oct 2004
Posts
11
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
}```

9. 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>```

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

X vBulletin 4.2.2 Debug Information

• Page Generation 0.13480 seconds
• Memory Usage 2,922KB
• Queries Executed 13 (?)
More Information
Template Usage (33):
• (1)SHOWTHREAD
• (1)ad_footer_end
• (1)ad_footer_start
• (1)ad_global_above_footer
• (1)ad_global_below_navbar
• (1)ad_global_header1
• (1)ad_global_header2
• (1)ad_navbar_below
• (1)ad_showthread_firstpost_sig
• (1)ad_showthread_firstpost_start
• (1)ad_thread_first_post_content
• (1)ad_thread_last_post_content
• (7)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (1)header
• (1)headinclude
• (1)headinclude_bottom
• (9)memberaction_dropdown
• (1)navbar
• (4)navbar_link
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (9)postbit
• (9)postbit_onlinestatus
• (9)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
• showthread
Included Files (26):
• ./showthread.php
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/functions_navigation.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_facebook.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (71):
• init_startup
• friendlyurl_resolve_class
• 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_threadinfo_query
• fetch_threadinfo
• fetch_foruminfo
• load_show_variables
• load_forum_show_variables
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• showthread_start
• showthread_getinfo
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• fetch_musername
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• showthread_post_start
• showthread_query_postids
• showthread_query
• bbcode_fetch_tags
• bbcode_create
• showthread_postbit_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete
• build_navigation_data
• build_navigation_array
• check_navigation_permission
• process_navigation_links_start
• process_navigation_links_complete
• set_navigation_menu_element
• build_navigation_menudata
• build_navigation_listdata
• build_navigation_list
• set_navigation_tab_main
• set_navigation_tab_fallback
• navigation_tab_complete
• fb_like_button
• showthread_complete
• page_templates