java newbie... sort array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: java newbie... sort array

  1. #1
    Join Date
    Sep 2002
    Posts
    2

    java newbie... sort array

    How do I sort an array of "-" delimited numbers?

    The array will contain numbers like this:

    0-471-34609-8
    0-13-031997-X
    .
    .
    .

    Any clue on how I can sort that?

    thanks,

    Paul

  2. #2
    Join Date
    Sep 2002
    Posts
    2
    nevermind... I'm real new w/ java...


    java.util.Arrays.sort(myArray);




  3. #3
    Join Date
    Aug 2002
    Posts
    94
    Hi,
    one option is to read the numbers like string and remove "-" from it and convert it back to numbers and sort (like ordinary numbers).

    The other option if format is fixed for all numbers is to look at the first part of the number and compare i.e if the numbers are

    0-471-34609-8
    1-13-031997-6

    Then comapring the first digit (0 & 1) yields the order regardless of the remaining digits. If the first digit is same then go to the second one and so on till order is sorted out but beware if the format is not fixed then it will not work e.g

    *- 471-34609-8
    1-13-031997-6

    in this case since the format is different the second number though bigger may end up as a smaller number in comparison.

    bye

    Pankaj

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center