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
对列表(Double,(String,List[String])先按Double排序,然后按元组中的第一个字符串值排序_List_Scala_Sorting - Fatal编程技术网

对列表(Double,(String,List[String])先按Double排序,然后按元组中的第一个字符串值排序

对列表(Double,(String,List[String])先按Double排序,然后按元组中的第一个字符串值排序,list,scala,sorting,List,Scala,Sorting,使用内置函数按两个条件对scala中的列表进行排序 输入: List((1.2 , (C, List(A))), (0.7 , (B, List(A))), (0.9 , (D, List(M))), (1.2 , (E, List(D,F))), (0.5 , (A, List(A,C))), (0.0 , (N, List(Z)))) List((1.2 , (C, List(A))), (1.2 , (E, List

使用内置函数按两个条件对scala中的列表进行排序

输入:

List((1.2 , (C, List(A))), 
     (0.7 , (B, List(A))), 
     (0.9 , (D, List(M))), 
     (1.2 , (E, List(D,F))), 
     (0.5 , (A, List(A,C))), 
     (0.0 , (N, List(Z))))
List((1.2 , (C, List(A))), 
     (1.2 , (E, List(D,F))), 
     (0.9 , (D, List(M))), 
     (0.7 , (B, List(A))) , 
     (0.5 , (A, List(A,C))), 
     (0.0 , (N, List(Z))))

如果第一个双精度值相等,则使用第一个字符串值进行排序。 就像有两个1.2的值一样,然后检查C和E

输出:

List((1.2 , (C, List(A))), 
     (0.7 , (B, List(A))), 
     (0.9 , (D, List(M))), 
     (1.2 , (E, List(D,F))), 
     (0.5 , (A, List(A,C))), 
     (0.0 , (N, List(Z))))
List((1.2 , (C, List(A))), 
     (1.2 , (E, List(D,F))), 
     (0.9 , (D, List(M))), 
     (0.7 , (B, List(A))) , 
     (0.5 , (A, List(A,C))), 
     (0.0 , (N, List(Z))))


为了获得请求的订单,我执行了以下操作:

input.sortWith{case (a,b) => a._1 > b._1 ||
                             a._1 == b._1 &&
                             a._2._1 < b._2._1}
input.sortWith{case(a,b)=>a.\u 1>b.\u 1||
a、 _1==b._1&&
a、 _2._1
您可以这样使用
sortBy

input.sortBy{ case (d, (s, _)) => (-d, s) }
这将从输入中提取一个新的数据值,然后根据该值进行排序


这可能比
sortWith
更简单,因为您只需要提供类型,而不是每个类型的比较函数。

您尝试了什么?为什么它不起作用?你查过电话号码了吗。