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]
# 1

> 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.

~

yawmarka at 2007-7-29 17:09:08 > top of Java-index,Java Essentials,Java Programming...
# 2

Your code smells bad. It's the smell of object denial. Get rid of those

parallel arrays and invest in a class.

BigDaddyLoveHandlesa at 2007-7-29 17:09:08 > top of Java-index,Java Essentials,Java Programming...
# 3

Dear Friends,

Thanks a lot for ur valuable suggestions. Actually I'm working with the same logic in SAP where one of my queue(array) is dependant on another.

Best regards,

raj.

Raj@SAPa at 2007-7-29 17:09:08 > top of Java-index,Java Essentials,Java Programming...
# 4

#! /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())

}

~

yawmarka at 2007-7-29 17:09:08 > top of Java-index,Java Essentials,Java Programming...
# 5

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?

BigDaddyLoveHandlesa at 2007-7-29 17:09:08 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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.

~

yawmarka at 2007-7-29 17:09:08 > top of Java-index,Java Essentials,Java Programming...
# 7

> Why not just use Groovy? It's as simple as putting a JAR in your classpath.

What JAR? I thought Groovy was part of J2SE 1.6?

BigDaddyLoveHandlesa at 2007-7-29 17:09:08 > top of Java-index,Java Essentials,Java Programming...
# 8

> What JAR?

http://groovy.codehaus.org/Download

Get ready for the love, Mr. Handles. :o)

~

yawmarka at 2007-7-29 17:09:08 > top of Java-index,Java Essentials,Java Programming...