Logic to sort arrays
Dear Friends,
I've doubt in sorting arrays .I'll tell u guys the problem , provide some logics for it.
I have two arrays say First Array has {AAA,FFF,GGG,BBB,LLL} SecondArray has{Prod,Dev,Line,Test,Move} .
I want to sort the First Array so that the second array also need to be sorted on the basis of First Array.
The output I needed is
First Arr--{AAA,BBB,FFF,GGG,LLL}
Sec Arr--{Prod,Test,Dev,Line,Move}
Hope the explanations are clear.
Best regards,
raj.
[520 byte] By [
Raj@SAPa] at [2007-11-27 11:36:30]

> Hope the explanations are clear.
Not really, but let me see if I have it straight. The same changes applied index-wise to the first array must be applied to the second array? If so, I'd suggest that two arrays isn't the best way to model this data, and recommend a Map instead.
~
#! /usr/bin/groovy
a1 = ['AAA','FFF','GGG','BBB','LLL']
a2 = ['Prod','Dev','Line','Test','Move']
// put all the elements in a sorted map...
def sorted = new TreeMap()
0.upto(a1.size - 1) { idx ->
sorted[a1[idx]] = a2[idx]
}
assert keys(sorted)== ['AAA','BBB','FFF','GGG','LLL']
assert values(sorted) == ['Prod','Test','Dev','Line','Move']
def keys(map) {
return new ArrayList(map.keySet())
}
def values(map) {
return new ArrayList(map.values())
}
~
> That groovy is making me wish Java had literal
> notations for lists, sets and maps.
> (Not to mention arrays :^)
> Are there any rumblings of that appearing in a later
> version of Java?
Why not just use Groovy? It's as simple as putting a JAR in your classpath.
~