sorting

Insertion sort array of structure by 2 fields

人走茶凉 提交于 2021-01-24 11:45:03
问题 I am making an uno card game with structure of card with fields: struct card { int rank char *color char *action. } I am able to make it sort by color with insertion. I wonder how could I first sort the array of uno card by color, then sort the rank of each color. 回答1: Thanks to Bo Persoson, this is the solution to my question void sort(card *a, int length) { int j; for (int i = 1; i < length; i++) { j = i; while (j > 0 && a[j].color < a[j - 1].color || (a[j].color == a[j - 1].color && a[j]

Insertion sort array of structure by 2 fields

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-24 11:44:26
问题 I am making an uno card game with structure of card with fields: struct card { int rank char *color char *action. } I am able to make it sort by color with insertion. I wonder how could I first sort the array of uno card by color, then sort the rank of each color. 回答1: Thanks to Bo Persoson, this is the solution to my question void sort(card *a, int length) { int j; for (int i = 1; i < length; i++) { j = i; while (j > 0 && a[j].color < a[j - 1].color || (a[j].color == a[j - 1].color && a[j]

How to sort two dimensional array lexicographically?

℡╲_俬逩灬. 提交于 2021-01-23 06:58:38
问题 Assuming we have a two-dimensional array as follows: int[][] source = { { 3, 5, 6, 1}, { 3, 3, 5, -6}, { -1, -3, -5, -6}, { 124, 43, 55, -66} }; how do we sort the multidimensional array source lexicographically ? So, as a result, I'd expect it to be: [ [ -1, -3, -5, -6], [ 3, 3, 5, -6], [ 3, 5, 6, 1], [124, 43, 55, -66] ] a lot of questions on this site seem to only suggest sorting by the first element of each array or second, third etc. but not taking in consideration the entire array. 回答1:

How does std::sort work for list of pairs?

不问归期 提交于 2021-01-22 03:57:01
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

How does std::sort work for list of pairs?

折月煮酒 提交于 2021-01-22 03:56:52
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

How does std::sort work for list of pairs?

僤鯓⒐⒋嵵緔 提交于 2021-01-22 03:53:57
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

How does std::sort work for list of pairs?

泄露秘密 提交于 2021-01-22 03:52:46
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

Kotlin sorting nulls last

谁说胖子不能爱 提交于 2021-01-21 12:04:07
问题 What would be a Kotlin way of sorting list of objects by nullable field with nulls last? Kotlin object to sort: @JsonInclude(NON_NULL) data class SomeObject( val nullableField: String? ) Analogue to below Java code: @Test public void name() { List<SomeObject> sorted = Stream.of(new SomeObject("bbb"), new SomeObject(null), new SomeObject("aaa")) .sorted(Comparator.comparing(SomeObject::getNullableField, Comparator.nullsLast(Comparator.naturalOrder()))) .collect(toList()); assertEquals("aaa",

Kotlin sorting nulls last

我们两清 提交于 2021-01-21 12:03:13
问题 What would be a Kotlin way of sorting list of objects by nullable field with nulls last? Kotlin object to sort: @JsonInclude(NON_NULL) data class SomeObject( val nullableField: String? ) Analogue to below Java code: @Test public void name() { List<SomeObject> sorted = Stream.of(new SomeObject("bbb"), new SomeObject(null), new SomeObject("aaa")) .sorted(Comparator.comparing(SomeObject::getNullableField, Comparator.nullsLast(Comparator.naturalOrder()))) .collect(toList()); assertEquals("aaa",

How to sort by a field of class with its own comparator?

喜欢而已 提交于 2021-01-21 10:31:55
问题 I have a sample class, say Country : class Country { public String name; public int population; public Flag flag; ... } I have this Flag class defined somewhere else class Flag { String id; int pixel; ... } Now I create a separate comparator DefaultFlagRankingComparator() that can sort Flag by id. How can I sort a list of Country by Flag id, using this DefaultFlagRankingComparator()? 回答1: You can invoke the compare method of the Comparator with the flag field of each country.