sort()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    #16
    Re: sort()

    That works pretty consistently. For me, I don't care the order in which the
    sort comes out but I need it to be consistent with the naming convention
    amongst what the server serves up and the application I am writing. This
    seems to do it.

    Thanks to all that helped.

    David





    "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
    news:ll6uerv4.f sf@hotpop.com.. .[color=blue]
    > "David" <right@dd.com > writes:
    >[color=green]
    > > There needs to be a way to sort them so the result is always like this..
    > >
    > > blueimage2.jpg, darkimage1.jpg, darkimage10.jpg ,lightimage1.jp g[/color]
    >[color=green]
    > > It sorts the image names alphanumericall y but .. those that have[/color][/color]
    numerical[color=blue][color=green]
    > > endings will be sorted in the correct order. Hard to explain but I[/color][/color]
    think[color=blue][color=green]
    > > you know what I mean. Is this possible?[/color]
    >
    > Everything is possible, if you can specify the desired outcome clearly.
    >
    > A very general method for comparing strings containing numbers is
    > numCmp here:
    > ---
    > function cmp(a,b) { // standard comparison.
    > return (b<a)-(a<b);
    > }
    >
    > function numCmp(a,b) {
    > var re1 = /(\d+)|\D+/g;
    > var re2 = /(\d+)|\D+/g;
    > re1.lastIndex = 0;re2.lastIndex =0; // Opera 8 bug.
    > var res = 0;
    > do {
    > match1 = re1.exec(a);
    > match2 = re2.exec(b);
    > if (match1) {
    > if (match2) {
    > if (match1[1]) {
    > if (match2[1]) { // fully numeric.
    > res = Number(match1[1]) - Number(match2[1]) ||[/color]
    cmp(match1[0],match2[0]);[color=blue]
    > } else {
    > res = -1;
    > }
    > } else {
    > if (match2[1]) {
    > res = 1;
    > } else {
    > res = cmp(match1[0],match2[0]);
    > }
    > }
    > } else {
    > res = 1;
    > }
    > } else {
    > if (match2) {
    > res = -1;
    > } else {
    > res = 0; break;
    > }
    > }
    > } while (res == 0);
    > return res;
    > }
    >
    > ---
    >
    > It splits the strings into blocks of digits and non-digits respectively,
    > and then compares the digit-blocks as numbers. More precisely, it sorts
    > as if each digit block was one character that comes before any normal
    > character. If the numbers are identical, their string representation is
    > compared normally to disambiguate, i.e., "ab01" comes before "ab1".
    >
    > Example:
    > ---
    > var l = ["ab10", "ab!", "ab2", "ab02", "ab0"];
    > var ls = l.sort(numCmp);
    > alert(ls); // ab0, ab02, ab2, ab10, ab!
    > ---
    >
    > It's not a very effective way to sort, since each comparison splits
    > both strings into blocks again. For efficiency, you could split the
    > strings into blocks first, so it's only done once per string, and then
    > sort using these arrays as keys.
    >
    > Good luck.
    > /L
    > --
    > Lasse Reichstein Nielsen - lrn@hotpop.com
    > DHTML Death Colors:[/color]
    <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
    > 'Faith without judgement merely degrades the spirit divine.'[/color]


    Comment

    Working...