/    Sign up×
Community /Pin to ProfileBookmark

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)
}

[/CODE]

Regards,
jmcall10 ?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@iBeZiJan 26.2013 — You're only sorting by the name, you need to sort by the value as well.

<i>
</i>function(a, b){
var nameA=a.myname.toLowerCase(), nameB=b.myname.toLowerCase()
var valA=a.value, valB=b.value;
if (nameA &lt; nameB || valA &lt; valB) //sort string ascending
return -1
if (nameA &gt; nameB || valA &gt; valB)
return 1
return 0 //default return value (no sorting)
}
Copy linkTweet thisAlerts:
@jmcall10authorJan 26.2013 — 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
Copy linkTweet thisAlerts:
@iBeZiJan 26.2013 — What format are the names/values your sorting in? I'm assuming it's an array of objects like this

<i>
</i>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

<i>
</i>array.sort(
function(a, b){
var nameA=a.myname.toLowerCase(), nameB=b.myname.toLowerCase()
var valA=a.value, valB=b.value;
if (nameA &lt; nameB || valA &lt; valB) //sort string ascending
return -1
if (nameA &gt; nameB || valA &gt; valB)
return 1
return 0 //default return value (no sorting)
}
);
Copy linkTweet thisAlerts:
@jmcall10authorJan 26.2013 — Yes effectively that is what is happening.

However the value is within the myname variable. for example "Apple - 1.0051" is the full myname.
Copy linkTweet thisAlerts:
@iBeZiJan 26.2013 — This should do it

<i>
</i>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 &lt; nameB || valA &lt; valB) //sort string ascending
return -1
if (nameA &gt; nameB || valA &gt; valB)
return 1
return 0 //default return value (no sorting)
}
Copy linkTweet thisAlerts:
@jmcall10authorJan 26.2013 — 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.
Copy linkTweet thisAlerts:
@jmcall10authorJan 26.2013 — 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
}[/CODE]
Copy linkTweet thisAlerts:
@JMRKERJan 26.2013 — Glad you figured it out. Took me too long to replicate you data. Here is an alternative method...
<i>
</i>&lt;div id="debugger"&gt;&lt;/div&gt;

&lt;script type="text/javascript"&gt;
// 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() &gt; b.name.toLowerCase(); } //no need for an array index with objects

function displayMarkersObj(arr){
var str = "";
for(var i = 0; i &lt; arr.length; i++) {
str += "&lt;br&gt;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 += '&lt;p&gt;Distance sort:'+str;

rmarkers.sort(sortAlphaObj);
str = displayMarkersObj(rmarkers);
document.getElementById('debugger').innerHTML += '&lt;p&gt;Name sort:'+str;
&lt;/script&gt;
×

Success!

Help @jmcall10 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...