简体   繁体   English

字符串排序数组

[英]Sorting array of strings

I am developing a project in android. 我正在用android开发一个项目。 I have five arrays of type String, with each item in the array related to the respective index of the remaining arrays. 我有五个String类型的数组,数组中的每个项目都与其余数组的相应索引有关。 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. 我以为可以使用HashMap来实现相同的功能,但是不确定如何执行。

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 请检查以下问题: 如何在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. 您的描述不是很清楚,但是似乎您需要一个带有“ QR Code”,“ Name”等字段的类。一旦有了它,您将拥有一个充满该类实例的单个数组,而不是5单独的数组。

Now you can sort the array using a Comparator to put any ordering you want on it. 现在,您可以使用Comparator对数组进行排序,以对数组进行排序。

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 这里有一个很好的例子: Android sort array

Search for words like java sort array comparable in order to get more on the subject. 搜索类似java排序数组之类的单词,以获取更多有关该主题的信息。

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: 您应该使用一个对象来存储与每个实体相对应的所有数据,将每个对象放入Collection或Array中,然后使用Comparable接口对它们进行排序:

public class MyData implements Comparable<MyData> {

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

//more code...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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