1

I have three arrays:

appNames
packageNames 
appIcons

Here app names is a string, package names are also string but package name and appIcons are of file type. I want to display all these three things in a list, but in the order of appNames.

Now, I want to sort this array in alphabetical order and also other two arrays, depending on this.

I am working in Java, and looking for some easy approach for it. PHP has something called array_multisort().

learner
  • 4,614
  • 7
  • 54
  • 98
  • Is app names a list of type `String`? If so, then you can use `Arrays.sort()`. If not, implement your own custom `Comparator`. – Idos May 30 '16 at 10:20
  • @Idos that would be `Arrays.sort` in this case. – Mena May 30 '16 at 10:20
  • Yes that is a `string`. – learner May 30 '16 at 10:20
  • @Idos whatever indeed. Voting to close anyway :) – Mena May 30 '16 at 10:21
  • 1
    and `Java` has called something called `classes`, where you could define everything that is depending on an instance of this specific class, maybe called `App`, which has two `List`s, maybe called `packageNames` and `appIcons` where you could define the dependicies here and are only left with a single `List` which you would need to sort. – SomeJavaGuy May 30 '16 at 10:22
  • @KevinEsche I know that. I am looking for something easy approach. – learner May 30 '16 at 10:23
  • @Idos Can you give me any working example? – learner May 30 '16 at 10:24
  • @learner sticking to oop could actually leat to the "most easy solution", because that´s how one should use java – SomeJavaGuy May 30 '16 at 10:24
  • `Arrays.sort(appNames);`, `Arrays.sort(packageNames);` you get the point I think – Idos May 30 '16 at 10:24
  • @learner, good question. Unfortunately there's nothing in the standard API that will solve this for you directly. Your best option is probably to create a wrapper class (or a `Pair`, or in this case a [`Triple`](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/tuple/ImmutableTriple.html) from some library), pair up the objects, and sort the list of pairs. You might also want to consider using a `SortedMap` and use elements of the app names as keys. – aioobe May 30 '16 at 10:25
  • I don't completely understand the use case but you can club the 3 classes into 1 and sort that on the basis of appNames – Dexter May 30 '16 at 10:25
  • @aioobe maybe, because it is not well-formed, especially the "depending on this" part. Anyway good luck =] – Idos May 30 '16 at 10:26
  • @aioobe thank you for this. Someone voted my question for closing. I am trying to work on collections. Try your suggestion too. – learner May 30 '16 at 10:26
  • @Idos, if you don't understand the question, ask for a clarification. Otherwise your comments are just noise. – aioobe May 30 '16 at 10:28
  • Possible duplicate of [Java: Sort an array](http://stackoverflow.com/questions/8938235/java-sort-an-array) – Petter Friberg May 30 '16 at 16:39

2 Answers2

2

The native Java approach is to bundle the values from three parallel arrays into a single array or list of objects with three fields, sort it using custom comparer, and then re-distribute the values back into three arrays.

An approach that does not require a new class is to make an index array, sort it with a custom comparator, and use the resulting order of indexes to reorder the three arrays.

For example, if your arrays look like this

a = {"quick", "brown", "fox"};
b = { 5, 10, 1};
c = {.1, .7, .2};

your unsorted index array would look like this

i = {0, 1, 2} // Indexes of quick, brown, fox

your sorted index array would look like this

i = {1, 2, 0} // Indexes of brown, fox, quick

At this point, you can walk the sorted index array, and create new arrays for a, b, and c:

a1 = {"brown", "fox", "quick"}
b1 = {10, 1, 5}
c1 = {.7, .2, .1}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Not a super efficient way to sort, but if arrays aren't big, can do well the old way to order

for (int i=0; i< appNames.lenght; i++)}
 for (int j=i+1; j< appNames.lenght; j++){
    if (appNames[i].compare(appNames[j])<0){
      // switch i and j in all arrays
      String temp= appNames[i];
      appNames[i]=appNames[j];
      appNames[j]=temp;

      temp= packageNames[i];
      packageNames[i]=packageNames[j];
      packageNames[j]=temp;

      temp= appIcons[i];
      appIcons[i]=appIcons[j];
      appIcons[j]=temp;
    }
  }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Marco
  • 47
  • 4