简体   繁体   中英

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.

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM