Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1266

Re: Array sort problem.

From Warren Tang <nospam@tangcs.com>
Newsgroups comp.lang.java.help
Subject Re: Array sort problem.
Date 2011-10-19 10:59 +0800
Organization Aioe.org NNTP Server
Message-ID <j7leek$din$1@speranza.aioe.org> (permalink)
References <j7i0eo$kfs$1@speranza.aioe.org> <j7i5s3$7l6$1@dont-email.me> <11614444.1991.1318892090322.JavaMail.geo-discussion-forums@prmr25>

Show all headers | View raw


On 10/18/2011 6:54 AM, Lew wrote:
> Alex Mentis wrote:
>> Warren Tang wrote:
>>> I have an array:
>>>
>>> index value
>>> 0	33
>>> 1	22
>>> 2	44
>>> 3	11
>>>
>>> Now I'd like to sort it, but I also need to preserve the original
>>> index, like this:
>>>
>>> newIndex originalIndex sortedValue
>>> 0	3	11
>>> 1	1	22
>>> 2	0	33
>>> 3	2	44
>>>
>>> How can this be done conveniently in Java?
>>
>> I imagine you'd have to go through the array of values and turn it into
>> an array of objects that contain fields for both the original index and
>> value. Then sort the new array based on the values.
>
> +1
>
> Here's a rough outline of such a (value,index) type (not compiled, untried):
>
>   public class ValueIndex<T extends Comparable>
>          implements Comparable<ValueIndex<T>>
>   {
>     private final T value;
>     private final int index;
>     public ValueIndex( T val, int idx )
>     {
>       if (val == null) {throw new IllegalArgumentException("null value");}
>       this.value = val;
>       this.index = idx;
>       assert this.value != null;
>     }
>     public T getValue() {assert value != null; return value;}
>     public int getIndex() {return index;}
>     @Override public int compareTo(ValueIndex<T>  other)
>     {
>       return other == null ? 1 : getValue().compareTo( other.getValue() );
>     }
>   }
>
>

I had thought to swap the "keys" and "values", and the use a TreeMap to 
sort it as follows:

     int[] scores = {33, 22, 44, 11};
     TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
     for(int i = 0; i < scores.length; i++) {
       map.put(scores[i], i);
     }
     int j = 0;
     for(Entry<Integer, Integer> e : map.entrySet()) {
       System.out.println(String.format("%10d%10d%10d", j++, 
e.getValue(), e.getKey()));
     }

But it won't work if "values" have duplicates. So I have to define a new 
class after all. Thank you guys for the answer.

Regards,
Warren Tang

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Array sort problem. Warren Tang <nospam@tangcs.com> - 2011-10-18 03:42 +0800
  Re: Array sort problem. "Alex Mentis" <foo@invalid.invalid> - 2011-10-17 21:14 +0000
    Re: Array sort problem. Lew <lewbloch@gmail.com> - 2011-10-17 15:54 -0700
      Re: Array sort problem. Warren Tang <nospam@tangcs.com> - 2011-10-19 10:59 +0800
  Re: Array sort problem. Roedy Green <see_website@mindprod.com.invalid> - 2011-10-19 15:22 -0700

csiph-web