Help in sorting
In the database I have region data stored as shown below:
ID PARENT_ID REGION
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.
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:
Originally Posted by nn12
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.
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.
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.