• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help! Sorting problem

 
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a string array:
dataList[]
and dataList.length >= 0
it contains every field in a column of a table
like:
Mushroom
Mushroom
Cake
Pizza
Pizza
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?
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dont know if a get the question, but try out Arrays.sort() method

//Hope it helps Rille
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
reply
    Bookmark Topic Watch Topic
  • New Topic