Dayanand
Dayanand

Reputation: 5

Sorting array of strings

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.

Upvotes: 0

Views: 192

Answers (4)

Averroes
Averroes

Reputation: 4228

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

Upvotes: 0

dty
dty

Reputation: 18998

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.

Upvotes: 2

Leonel Machava
Leonel Machava

Reputation: 1531

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

Upvotes: 2

Warpzit
Warpzit

Reputation: 28152

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.

Upvotes: 0

Related Questions