Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1260
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
|---|---|
| From | Lew <lewbloch@gmail.com> |
| Newsgroups | comp.lang.java.help |
| Subject | Re: Array sort problem. |
| Date | Mon, 17 Oct 2011 15:54:50 -0700 (PDT) |
| Organization | http://groups.google.com |
| Lines | 52 |
| Message-ID | <11614444.1991.1318892090322.JavaMail.geo-discussion-forums@prmr25> (permalink) |
| References | <j7i0eo$kfs$1@speranza.aioe.org> <j7i5s3$7l6$1@dont-email.me> |
| Reply-To | comp.lang.java.help@googlegroups.com |
| NNTP-Posting-Host | 2620:0:1000:2004:224:d7ff:fe69:5838 |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Trace | posting.google.com 1318892090 30509 127.0.0.1 (17 Oct 2011 22:54:50 GMT) |
| X-Complaints-To | groups-abuse@google.com |
| NNTP-Posting-Date | Mon, 17 Oct 2011 22:54:50 +0000 (UTC) |
| In-Reply-To | <j7i5s3$7l6$1@dont-email.me> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=2620:0:1000:2004:224:d7ff:fe69:5838; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T |
| User-Agent | G2/1.0 |
| X-Google-Web-Client | true |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.help:1260 |
Show key headers only | View raw
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() );
}
}
--
Lew
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
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