SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: sorting arrays

  1. #1
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorting arrays

    I had two corresponding arrays (train numbers and names). I figured out for myself how to merge them so that I now have something alone the lines of
    (70000, "Britannia", 70001, "Lord Hurcomb", 70002, "Geoffrey Chaucer", 70003, "John Bunyan", 70004, "William Shakespeare", 70005, "John Milton") etc etc.

    What I now want to do is SORT the array - but only one the names i.e every alternate part of the array [1], [3], [5], [7] etc - and retain the original pairings - so I would end up with a new array like Britannia, 70000, Geoffrey Chaucer, 70002, John Bunyan. 70003, John Milton, 70005, William Shakespeare, 70004, etc etc.

    I figured out the original merge myself (pat on the back!) but am racking my brains and looking everywhere on teh web for some hint on how to go about this slightly unusual kind of sort. You don't need to provide a total solution (I try and figure things out for myself where possible as it's the best way to learn) - but any hints would be appreciated!!!

    nick

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, I think you need to change your data structure. I suggest something along these lines:

    Code JavaScript:
    var trains = [
        { name: "Britannia", number: 70000 },
        { name: "Lord Hurcomb", number: 70001 },
        /// etc...
    ];

    Then, it's much easier to sort using the native array sort() method:

    Code JavaScript:
    trains.sort(function(a,b){
        return a.number - b.number;
    });
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I see what you're saying and the only reason I didn't do that is because I already had several arrays in the original format and rewriting themn would have taken ages! I've came up wrth a good way of making a third array by doing - for (i=0; i<55; i++) in array 1 and array 2, interleaving all the elemnts in the two arrays to make a third array. I can document.write this third array then cut&paste it imy code - but I'm sure there must be a better way of saving the resulting array as a separate entity without resorting to this rather complicated method!

    cheers for any hints

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's tons of ways to go about it, but if you must keep the array structure you have, I would just convert it to the other structure, sort it using Jimmyp's method, then convert it back to the original structure.

  5. #5
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, guys, i think you are right and I will have a go at restructing along the lines you suggest. At least I don't have to type out all the names and numbers again !

    nick


Bookmarks

Posting Permissions

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