Results 1 to 3 of 3
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: Sorting arrays

  1. #1
    Blondedude092 is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Sorting arrays

    I would like to know how can I sort one array alphabetically while sorting other arrays by that arrays' index.

    for example the first array contains three names; "aaron", "jill", "bob"
    the second array would have three names also; "olson", "taylor", "foster"

    when it sorts the first array it will sort it into "aaron", "bob", "jill" but i want it to also sort the second one the same way it sorted the first one but based on how it sorted the first one. so it would result in "olson", "foster", "taylor".

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

    Default Re: Sorting arrays

    There are possibly many ways to do this. Given java is object oriented, I'd suggest to create a class which contains two values - and sort an array of objects of that class based upon one of those values.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Sorting arrays

    There's a bit of a problem in knowing how to respond to this. If it's a homework question, perhaps you could post it because how you tackle this depends a lot on the circumstances (what you know, and what the question is getting you to practice).

    Anyway there are two broad ways of approaching it:

    A) Don't use "parallel arrays" - that is, instead of splitting the data between two arrays write a Name class that includes both a first name and a surname. And have a single array of Name instances. Then you sort the array using the sort() method of the Arrays class. As the library sort() method does its thing, sorting alphabetically on the first names, the surnames get carried along for the ride.

    B) Write some code that actually sorts the first name array by shuffling its elements about using some sorting algorithm you know about (or find out about: Wikipedia is your friend). Then alter that code so that it each time it does something to the first name array it does the analogous thing to the surname array. The place to start here is with some solid (compiled and tested) code to implement the first name sort.

    Most people would prefer (A) since money and time has been spent getting the library sort() method fast and accurate. And since keeping the two parts of the name together will make sense in all sorts of other contexts (sorting by surname, printing the full name, etc). But, as I said, much depends on the context within which the question has arisen.
    wsaryada and doWhile like this.

Similar Threads

  1. Sorting arrays
    By Lex in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 03:19 AM
  2. Help with sorting arrays
    By Joycey in forum New To Java
    Replies: 4
    Last Post: 03-30-2010, 09:35 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  4. Sorting Two Arrays
    By Faye Rett in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 02:00 AM
  5. Sorting Arrays by enum
    By sahity1a@yahoo.com in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 10:08 AM

Tags for this Thread

Posting Permissions

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