简体   繁体   English

如何对Arraylist进行排序 <String > 基于另一个arraylist <String> 在科特林

[英]How to sort Arraylist<String > based on another arraylist<String> in Kotlin

In my application I have 2 arraylists. 在我的应用程序中,我有2个arraylist。 One coming from server and another from assets. 一个来自服务器,另一个来自资产。 I want to sort server arraylist based on order of asset arraylist in Kotlin. 我想根据Kotlin中资产arraylist的顺序对服务器arraylist进行排序。 eg val serverArraylist= { “xyz”, “abc”, “pqr”} val assetsList = { “abc”, “pqr”, “xyz”} 例如val serverArraylist = {“ xyz”,“ abc”,“ pqr”} val assetList = {“ abc”,“ pqr”,“ xyz”}

Expected output should be, serverArraylist = { “abc”, “pqr”, “xyz”} 预期的输出应为serverArraylist = {“ abc”,“ pqr”,“ xyz”}

Both the list may not always contain same number of elements. 这两个列表可能并不总是包含相同数量的元素。 Sometimes serverlist may contain fewer elements But will not contain elements other than local list. 有时serverlist可能包含较少的元素,但不包含本地列表以外的元素。 Like, serverList= {xyz, pqr} Then expected list should be, severList= {pqr, xyz} 就像,serverList = {xyz,pqr}然后期望的列表应该是severList = {pqr,xyz}

Any help or idea highly appreciated. 任何帮助或想法高度赞赏。 Thanks in advance. 提前致谢。

I have had similar problems in the past where the ordering of list A is based on the ordering of list B. I dont think it will work if the lists are different sizes. 过去,列表A的排序基于列表B的排序时,我遇到过类似的问题。如果列表大小不同,我认为它不会起作用。 Otherwise, you need to have control of the 'swap' method in the sort, so that when list B elements are swapped, then so are the elements in list A. I dont know of a way to override this in Kotlin sort methods so in the past I wrote my own bubble sort method and swapped in both places. 否则,您需要控制排序中的'swap'方法,以便交换列表B元素时,列表A中的元素也要交换。我不知道在Kotlin排序方法中覆盖此方法的方法过去,我编写了自己的冒泡排序方法,并且在两个地方都进行了交换。 Other posters have given this which might help: java sorting with comparator and swap function 其他张贴者给出了以下信息可能会有所帮助: 使用比较器和交换功能对Java进行排序

When serverArraylist only contains elements also contained in assetsList , you don't need to sort any list. serverArraylist只包含也包含在元素assetsList ,你不需要任何排序名单。 The only thing you'd have to do is filter assetsList to only contain the elements of serverArraylist . 您唯一需要做的就是过滤assetsList使其仅包含serverArraylist的元素。

assetsList.filter { it in serverArraylist }
 val a = listOf("xyz","abc" ,"pqr" )
 val b = listOf("abc" ,"pqr", "xyz")
 val c = a + b
 val d = c.distinct().sorted()

This will give you 这会给你

[abc, pqr, xyz]

You could clear the serverArrayList and then addAll the elements from the assetsList to the serverArrayList. 您可以清除serverArrayList,然后将资产列表中的所有元素添加到serverArrayList中。

If you are unfamiliar with ArrayList operations you can find more HERE . 如果您不熟悉ArrayList操作,则可以在此处找到更多信息。

Edit: My apologies. 编辑:我的道歉。 I did not understand your question originally. 我本来不理解您的问题。 You could use a hashmap to help you keep track of the desired indexes and elements of serverArrayList. 您可以使用哈希图来帮助您跟踪所需的索引和serverArrayList的元素。 The process should be something like this: 该过程应如下所示:

  1. Iterate over serverArrayList 遍历serverArrayList
  2. For each element in serverArrayList get it's corresponding index from assetsList with indexOf(). 对于serverArrayList中的每个元素,使用indexOf()从AssetsList中获取其对应的索引。
  3. Put each pair (index, element) into a hash map. 将每对(索引,元素)放入哈希映射。
  4. Use the hash map to populate the new serverArrayList. 使用哈希图填充新的serverArrayList。

Look out for Out of Bounds errors as the index obtained could be larger than the size of serverArrayList, since you mentioned it could be smaller in size compared to assetsList. 请注意“越界”错误,因为获得的索引可能大于serverArrayList的大小,因为您提到的索引与资产列表相比可能更小。

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

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