1、构造题目所需的排序方法,例如根据 两数之差 从小到大排列:
1 2 3 4
| vector<vector<int>>tasks = {{1,3},{2,4},{10,11}, {10,12},{8,9}}; sort(tasks.begin(), tasks.end(), [&](vector<int> &a, vector<int> &b){ return a[1] - a[0] < b[1] - b[0]; });
|
2、根据第n个值来排序
tasks = [[2,3,1],[4,5,1],[1,5,2]]
1 2 3
| sort(tasks.begin(), tasks.end(), [](auto &a, auto &b) { return a[1] < b[1]; });
|
输出 [[2,3
,1],[4,5
,1],[1,5
,2]]
(Golang版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| task := [][]int{{2, 3, 1}, {4, 5, 1}, {1, 5, 2}} sort.Slice(tasks, func(i, j int) bool { return tasks[i][1] < tasks[j][1] })
task := [][]int{1,3,2},{2,5,3},{5,6,2} sort.Slice(tasks, func(i, j int) bool { return tasks[i][2] < tasks[j][2] })
sort.Slice(dictionary, func(i, j int) bool { a, b := dictionary[i], dictionary[j] if len(a) != len(b) { return len(a) > len(b) } return a < b })
|