Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Bubble Sort Array

    Hello, I need to sort this array in ascending order by ISBN number using a bubble sort. Can anyone help me? Thank you.

    Code:
    var books = [];
    books[0] = "1565923928:Programming In javascript:2290:950";
    books[1] = "2010948473:Java The Definitive Guide:1499:700";
    books[2] = "1098767854:Javascript For Beginners:999:600";

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You would be better off using an object or another array to avoid a split

    But as you asked:

    Code:
    var books = [];
    books[0] = "1565923928:Programming In javascript:2290:950";
    books[1] = "2010948473:Java The Definitive Guide:1499:700";
    books[2] = "1098767854:Javascript For Beginners:999:600";
    
    
    function isbn(a,b){
      var n1 = a.split(":")[0];
      var n2 = b.split(":")[0];
      return n1>n2
    }
    
    alert(books.sort(isbn).join("\n"));
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

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