0

I am developing a project in android. I have five arrays of type String, with each item in the array related to the respective index of the remaining arrays. Here I need to sort the single array, if I sort, all the respective index should replicate in the proper order as it was present in the sorted array.

Source code:

array1 = new String[qrcodelist.size()];
array2 = new String[name.size()];
array3 = new String[company.size()];
array4 = new String[date.size()];
array5 = new String[imageurl.size()];


for (int i = 0; i < name.size(); i++) {
    array1[i] = qrcodelist.get(i);
    array2[i] = name.get(i);
    array3[i] = company.get(i);
    array4[i] = date.get(i);
    array5[i] = imageurl.get(i);

}

I thought to implement the same using HashMap but was not sure on how to do it.

I request you to give me a logic to implement this task or any sample code.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Dayanand
  • 5
  • 3
  • 5
    so if you sort an array, each array is sorted the same way as the other? I would object that your data structure is strange. I'd rather have an object with 5 String fields, and then an array of this object, and therefore it is easy to sort it according to a comparator or another – njzk2 Aug 30 '12 at 09:22
  • 1
    Strange data structures... Be careful with IndexOutOfBoundsExceptions in that for loop. – Averroes Aug 30 '12 at 09:33
  • possible duplicate of [How to sort multiple arrays in java](http://stackoverflow.com/questions/12164795/how-to-sort-multiple-arrays-in-java) – PeeHaa Sep 02 '12 at 01:38

4 Answers4

2

I would advise you to review your data structure. I mean it seams not to be a good idea to split the data in separate arrays. All the related data should be contained in one object.

Please check this question: How to sort multiple arrays in java

Community
  • 1
  • 1
Leonel Machava
  • 1,501
  • 11
  • 19
2

It sounds to me like you're suffering from "object denial". Your description isn't very clear, but it seems like you need a class with fields for "QR Code", "Name", etc. Once you have that, you have a single array full of instances of that class, instead of 5 separate arrays.

Now you can sort the array using a Comparator to put any ordering you want on it.

dty
  • 18,795
  • 6
  • 56
  • 82
0

From what I understand I guess you just want to sort an array, next time search for it. There is a good example here: Android sort array

Search for words like java sort array comparable in order to get more on the subject.

Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155
0

You should use an object to store all the data corresponding to each entity, put each object in a Collection or an Array and then sort them implementing Comparable interface:

public class MyData implements Comparable<MyData> {

private String qrcodelist;
private String  name;
private String  company;
private String  date;
private String  imageurl;

//more code...
}
Averroes
  • 4,168
  • 6
  • 50
  • 63