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.

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 Commented Aug 30, 2012 at 9:22
  • 1
    Strange data structures... Be careful with IndexOutOfBoundsExceptions in that for loop. Commented Aug 30, 2012 at 9:33
  • possible duplicate of How to sort multiple arrays in java Commented Sep 2, 2012 at 1:38

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.