Results 1 to 4 of 4

Thread: Help in sorting

  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default Help in sorting

    In the database I have region data stored as shown below:

    ID PARENT_ID REGION
    1001 EUROPE
    1011 NORTH AMERICA
    1021 LATIN AMERICA
    1031 1001 EASTERN EUROPE
    1041 1001 WESTERN EUROPE

    In my java code I have a region objects with all the details. How do I sort this data such that every region is followed by its child region.

    For e.g
    EUROPE
    EASTERN EUROPE
    WESTERN EUROPE
    LATIN AMERICA
    NORTH AMERICA

    Any help would be appreciated.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nn12 View Post
    In the database I have region data stored as shown below:

    ID PARENT_ID REGION
    1001 EUROPE
    1011 NORTH AMERICA
    1021 LATIN AMERICA
    1031 1001 EASTERN EUROPE
    1041 1001 WESTERN EUROPE

    In my java code I have a region objects with all the details. How do I sort this data such that every region is followed by its child region.

    For e.g
    EUROPE
    EASTERN EUROPE
    WESTERN EUROPE
    LATIN AMERICA
    NORTH AMERICA

    Any help would be appreciated.
    You have to find a mapping from the key of the continent to a number to which the continents can be sorted, something like:

    map(1001) < map(1031) < map(1041) < map(1011) < map(1021)

    Do you have parent IDs for all continents? If so you can possibly catenate the parent ID to the ID. So the parent ID deterimines the major sorting order and the ID itself will be the minor sorting order. You have to fill in all the parent IDs though.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Might be overkill for something like this, but the structure you describe looks like a tree, so in the java code you can structure the data in this way (each region can have children) then traverse the tree in the appropriate direction to get the elements sorted in the appropriate way.

  4. #4
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Hi, can you please give me a code snippet to get started on this. I mean how to structure the data so that region can have child regions.
    Last edited by nn12; 01-08-2011 at 08:06 PM.

Similar Threads

  1. Sorting Problem
    By Freakzoyd in forum New To Java
    Replies: 5
    Last Post: 11-30-2010, 05:40 AM
  2. Sorting 10 numbers
    By perito in forum New To Java
    Replies: 56
    Last Post: 03-05-2009, 01:52 AM
  3. Sorting xml file
    By keioGirl in forum Advanced Java
    Replies: 4
    Last Post: 12-12-2008, 05:14 PM
  4. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 10:30 AM

Posting Permissions

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