/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Array Sort Routine check

While using the sort on an array I found that it produces an order of:
01….89AB….YZab….yz

I also found that I could sort numbers via their numeric value rather than their alphanumeric order,
(ie; 1 2 3 4 5 10 15 20 25 30 35 40 45 50
not 1 10 15 2 20 25 3 30 35 4 40 45 5 50)
with a slight modification to the sort function call.

Could someone tell me if I have overlooked some condition with this sort routine that follows?
My attempt was to sort the array elements similar to the normal sort
except that upper and lower case conditions were ignored
and the inclusion of a numeric sort order was included.

[code=php]
function AlphaNumSort(m,n) {
a = m.toLowerCase();
b = n.toLowerCase();
if (a == b) { return 0; } // values are equal

if (isNaN(m) || isNaN(n)) { // one or both are not numbers
return (a>b?1:-1);
} else {
return m-n; // both are numbers
}
}
……
// Used to sort randomized array like this:
NumStrArray.sort(AlphaNumSort);
[/code]

It seems to work with all conditions I can come up with, ?
but I don’t know if I’ve thought it through enough.

Any comments?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorDec 04.2007 — Yes, you rediscovered the wheel ? ( no offence, just kiddin' ) :-)

The [B]sort[/B] method does a "string oriented" sorting, not a "decimal numbered" one (because, for the processor, the base 10 has no special meaning, it is just a base among the others), thus to perform a "human understandable sorting" you must, indeed, to compare again, the array's members' absolute value (so to say: to bring the sorting at a bi-univoque binar operation, which the processor understands).
<i>
</i>[I]array[/I].sort(function (a,b) {return a[0] == b[0] ? 0 : (a[0] &lt; b[0] ? -1 : 1)})
Copy linkTweet thisAlerts:
@mrhooDec 04.2007 — if you want to sort mixed strings and numbers, so that 'file2' appears before 'file10' you need to do a bit more sorting.

[CODE]allSort= function(a, b) {
if(isFinite(a) && isFinite(b)) return a-b;
try{
var cnt= 0, tem;
a= (a+'').toLowerCase();
b= (b+'').toLowerCase();
if (a== b) return 0;
var Rx= /^(.)?d/;
var L= Math.min(a.length, b.length) + 1;
while (cnt < L &&
a.charAt(cnt)=== b.charAt(cnt) &&
Rx.test(b.substring(cnt))== false) {
cnt++;
}
a= a.substring(cnt);
b= b.substring(cnt);
if (Rx.test(a) || Rx.test(b)) {
if (Rx.test(a)== false) return a ? 1 : -1;
else if (Rx.test(b)== false) return b ? -1 : 1;
else {
var tem= parseFloat(a) - parseFloat(b);
if (tem != 0) return tem;
else tem= (parseFloat(a) +'').length;
a= a.substring(tem);
b= b.substring(tem);
}
}
}
catch(er){
return 0;
}
if (a== b) return 0;
return a > b ? 1 : -1;
}[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorDec 04.2007 — Thanks, Kor (I think ? ) and mrhoo.

While doing a search of the forum I guess I used the wrong terms as the cases I found only sorted alpha as 'AB..YZab..yz' instead of combining them as 'AabBCc...' or sorted only ALL numbers into the correct numeric sequence. I could not find an example of what I was trying to do that did both in the same routine.

Looks like I missed the 'file2' and 'file10' condition where the number was embedded inside the string.

Thanks for the viewing.
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...