File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Help! Sorting problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help! Sorting problem" Watch "Help! Sorting problem" New topic

Help! Sorting problem

HaoZhe Xu
Ranch Hand

Joined: Nov 03, 2003
Posts: 222
I have a string array:
and dataList.length >= 0
it contains every field in a column of a table
now i number it from 0 to 4
how to sort it in alphabetical order and store the sorted indexes in result[] which is a int array?

Rikard Qvarforth
Ranch Hand

Joined: Jul 10, 2001
Posts: 107
Dont know if a get the question, but try out Arrays.sort() method

//Hope it helps Rille
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
There are several ways to do this with verying performance and design elegance characteristics, but the main idea is to tag each String with its index before sorting.

One way would be to create a class implementing Comparable that stores the String and its natural (starting) index. Then you can use the Arrays.sort(Object[]) method to sort an array of those.

An old school way would be to create a second array of the indexes and write your own sort routine that rearranges the second array in the same way it does the array of Strings. But who writes their own sorting routines anymore?
I agree. Here's the link:
subject: Help! Sorting problem
It's not a secret anymore!