Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于另一个列表的排序列表';s指令_Java_List_Sorting_Kotlin - Fatal编程技术网

Java 基于另一个列表的排序列表';s指令

Java 基于另一个列表的排序列表';s指令,java,list,sorting,kotlin,Java,List,Sorting,Kotlin,我需要对Person对象的列表进行排序(list,其中每个Person对象几乎没有像id(唯一)、name、age…等属性) 排序顺序基于另一个列表。该列表包含一组人员id(已排序的列表) 使用Kotlin或Java以与id列表相同的顺序排列列表的最佳方法是什么 例如: List Person { (“ID1”,”PERSON1”,22,..), (“ID-2”,”PERSON2”,20,..) ), (“ID-3”,”PERSON3”,19,..),….. } 有序Id列表: List of

我需要对
Person
对象的列表进行排序(
list
,其中每个
Person
对象几乎没有像
id
(唯一)、
name
age
…等属性)

排序顺序基于另一个列表。该列表包含一组
人员
id
(已排序的
列表)

使用Kotlin或Java以与
id
列表相同的顺序排列
列表的最佳方法是什么

例如:

List Person {
(“ID1”,”PERSON1”,22,..), (“ID-2”,”PERSON2”,20,..) ), (“ID-3”,”PERSON3”,19,..),…..
}
有序Id列表:

List of ID {(“ID2”), (“ID1”),(”ID3”)….}
已排序的
人员
列表应为:

List PERSON {
 (“ID-2”,”PERSON 2”,20,..) ), (“ID1”,”PERSON 2”,22,..),  (“ID-3”,”PERSON 2”,19,..),…..
}
如果
Person
列表包含
id
列表中未提及的任何
id
,则这些值应位于已排序列表的末尾


编辑:这是我目前使用Java的方式。我希望有比这更好的方法:

public static List<Person> getSortList(List <Person> unsortedList, List<String> orderList){

    if(unsortedList!=null && !unsortedList.isEmpty() && orderList!=null && !orderList.isEmpty()){
        List sortedList = new ArrayList<OpenHABWidget>();
        for(String id : orderList){
            Person found= getPersonIfFound(unsortedList, id); // search for the item on the list by ID
            if(found!=null)sortedList.add(found);       // if found add to sorted list
            unsortedList.remove(found);        // remove added item
        }
        sortedList.addAll(unsortedList);        // append the reaming items on the unsorted list to new sorted list
        return sortedList;
    }
    else{
        return unsortedList;
    }

}

public static Person getPersonIfFound(List <Person> list, String key){
    for(Person person : list){
        if(person.getId().equals(key)){
            return person;
        }
    }
    return null;
}
publicstaticlist-getSortList(List-unsortedList,List-orderList){
if(unsortedList!=null&&!unsortedList.isEmpty()&&orderList!=null&&!orderList.isEmpty()){
List sortedList=新建ArrayList();
for(字符串id:orderList){
Person found=getPersonifound(未排序列表,id);//按id搜索列表中的项目
if(found!=null)sortedList.add(found);//if found添加到排序列表
unsortedList.remove(找到);//删除添加的项
}
sortedList.addAll(unsortedList);//将未排序列表上的铰孔项目追加到新的排序列表中
返回分类列表;
}
否则{
返回未排序列表;
}
}
公共静态Person GetPersonifound(列表,字符串键){
用于(个人:列表){
if(person.getId().equals(key)){
返回人;
}
}
返回null;
}
我会这样做(在伪代码中,因为我不知道您的代码是什么样子)


如果您有一个映射(id->person)而不是一个人物列表,那么查找就会更容易。

一个有效的解决方案是首先创建从
ids
(您所需的id顺序)中的id到该列表中的索引的映射:

val orderById = ids.withIndex().associate { it.value to it.index }
然后在此映射中,按照
id
的顺序对
人员列表进行排序:

val sortedPeople = people.sortedBy { orderById[it.id] }
注意:如果一个人的ID在
ID
中不存在,他们将被放在列表的第一位。要将它们放在最后,可以使用比较器:

val sortedPeople = people.sortedWith(compareBy(nullsLast<String>) { orderById[it.id] })
val sortedPeople=people.sortedWith(compareBy(nullsLast){orderById[it.id]})
尝试下面的代码

 Map<String, Person> personMap=new HashMap<>(); // create a map with key as ID and value as Person object
List<String> orderList=new ArrayList<>();  // create a list or array with ID order
List<Person> outputList=new ArrayList<>(); //list to hold sorted values

//logic

    // to sort Person based on ID list order
    for (String order : orderList) {
      if(personMap.containsKey(order)){
        outputList.add(personMap.get(order));
        personMap.remove(order);
      }
    }

  // logic to add the Person object whose id is not present in ID order list
    for (Entry<String, Person> entry : personMap.entrySet())
    {
      int lastIndex=outputList.size();
      outputList.add(lastIndex, entry.getValue());
      lastIndex++;
    }
Map personMap=newhashmap();//创建一个以key作为ID,value作为Person对象的映射
List orderList=new ArrayList();//创建ID顺序为的列表或数组
List outputList=new ArrayList()//用于保存排序值的列表
//逻辑
//根据ID列表顺序对人员进行排序
for(字符串顺序:orderList){
if(人员地图集装箱(订单)){
add(personMap.get(order));
人员地图移除(命令);
}
}
//添加id不在id顺序列表中的Person对象的逻辑
for(条目:personMap.entrySet())
{
int lastIndex=outputList.size();
add(lastIndex,entry.getValue());
lastIndex++;
}

现在,
outputList
将具有您期望的值…

下面的代码将人员列表转换为
映射,其中键将是ID,值将是人员对象本身。此
映射将有助于快速查找。然后迭代ID的列表,从
映射中获取值,并添加到另一个
列表中

fun main(args: Array<String>) {
  // List of ID
  val listId = listOf(2, 1, 3)

  val list = listOf(Person(id = 1, name = "A"),
        Person(id = 2, name = "B"),
        Person(id = 3, name = "C"))

  val map: Map<Int, Person> = list.associateBy ({it.id}, {it})

  val sortedList = mutableListOf<Person>()

  listId.forEach({
      sortedList.add(map[it]!!)
  })

  sortedList.forEach({
     println(it)
  })
}

data class Person(val id: Int, val name: String)

向我们展示您的代码。您已经尝试了哪些方法来解决您的问题?您所说的“最佳”方法是什么意思?您的数据集有多大?对于一些琐碎的情况,你也可以使用暴力。对于id列表中的每个项目,在人员列表中查找相应的项目并附加到新列表中。如果数据集很大,则按ID的升序对人员列表进行排序。然后执行相同的操作,但使用二进制搜索按idI查找人员。如果不进行物理排序,我认为这是最好的。使用
人员
地图存储您的人员
Map
然后使用已经排序的id列表作为索引。
nullsFirst
这里影响空用户的顺序,而不是那些
orderById[id]
为空的用户。要指定键的空顺序,需要将可选的比较器传递给
compareBy
people.sortedWith(compareBy(nullsFirst()){orderById[it.id]})
。这个比较器是可选的,因为如果没有它,空值首先被排序!
fun main(args: Array<String>) {
  // List of ID
  val listId = listOf(2, 1, 3)

  val list = listOf(Person(id = 1, name = "A"),
        Person(id = 2, name = "B"),
        Person(id = 3, name = "C"))

  val map: Map<Int, Person> = list.associateBy ({it.id}, {it})

  val sortedList = mutableListOf<Person>()

  listId.forEach({
      sortedList.add(map[it]!!)
  })

  sortedList.forEach({
     println(it)
  })
}

data class Person(val id: Int, val name: String)
Person(id=2, name=B)
Person(id=1, name=A)
Person(id=3, name=C)