オブジェクト降順の
12026 ワード
このチュートリアルでは、DART/Flutterの下にあるオブジェクトのリストを並べ替える方法と機能を紹介します
昇順に似ていますが、次のようにします. カスタム比較機能を使用する. 延長 延長 ポスト:Dart/Flutter Sort List of objects
カスタムの関数をリストに渡す
オブジェクトのリストを降順でソートできますreversed プロパティ.It returns an
しかし、大きなリストはどうですか?
別のアプローチを見てみましょう.
我々は不変のコレクションを作成することができますし、高速immutableコレクションライブラリを直接使用できます.
クラスは以前のアプローチと同じです
Dart/Flutter List Tutorial with Examples Dart/Flutter Constructors tutorial with examples Dart/Flutter String Methods & Operators tutorial with examples Dart/Flutter Future Tutorial with Examples Dart/Flutter Map Tutorial with Examples Dart/Flutter – Convert Object to JSON string Dart/Flutter – Convert/Parse JSON string, array into Object, List Dart/Flutter – Convert List to Map & Map to List
昇順に似ていますが、次のようにします.
Comparable
抽象クラスcompareTo()
方法及び使用reversed
. Comparable
抽象クラスcompareTo()
方法及び使用fast_immutable_collections 図書館.カスタム比較関数を使用しているオブジェクトの
カスタムの関数をリストに渡す
sort()
項目の場所を入れ替える方法.class Customer {
String name;
int age;
Customer(this.name, this.age);
@override
String toString() {
return '{ ${this.name}, ${this.age} }';
}
}
main() {
List<Customer> customers = [];
customers.add(Customer('Jack', 23));
customers.add(Customer('Adam', 27));
customers.add(Customer('Katherin', 25));
customers.sort((a, b) => b.age.compareTo(a.age));
print('Sort by Age: ' + customers.toString());
customers.sort((a, b) => b.name.compareTo(a.name));
print('Sort by Name: ' + customers.toString());
}
出力:Sort by Age: [{ Adam, 27 }, { Katherin, 25 }, { Jack, 23 }]
Sort by Name: [{ Katherin, 25 }, { Jack, 23 }, { Adam, 27 }]
同等の抽象クラスを使用しているオブジェクトのダート/フラッタソートリスト
オブジェクトのリストを降順でソートできますreversed プロパティ.It returns an
Iterable
リスト内のオブジェクトの.class Customer extends Comparable {
String name;
int age;
Customer(this.name, this.age);
@override
String toString() {
return '{ ${this.name}, ${this.age} }';
}
// sort by Name (asc)
@override
int compareTo(other) {
return this.name.compareTo(other.name);
}
}
main() {
List<Customer> customers = [];
customers.add(Customer('Jack', 23));
customers.add(Customer('Adam', 27));
customers.add(Customer('Katherin', 25));
customers.reversed.toList();
print(customers);
}
出力:[{ Katherin, 25 }, { Jack, 23 }, { Adam, 27 }]
第一に、我々はComparable
抽象クラスとオーバーライドcompareTo()
メソッドを呼び出しますlist.reversed.toList()
..reversed
リストを反転しません.代わりに、昇順で元のリストをソートし、Iterable
. だから我々は必要.toList()
最後に.しかし、大きなリストはどうですか?
別のアプローチを見てみましょう.
ライブラリを使用しているオブジェクトの
我々は不変のコレクションを作成することができますし、高速immutableコレクションライブラリを直接使用できます.
クラスは以前のアプローチと同じです
Comparable
オーバーライドcompareTo()
メソッド).でも使うsortReversed()
どちらがはるかに高速です.import 'package:fast_immutable_collections/fast_immutable_collections.dart';
class Customer extends Comparable {
...
@override
int compareTo(other) {
return this.name.compareTo(other.name);
}
}
main() {
List<Customer> customers = [];
customers.add(Customer('Jack', 23));
customers.add(Customer('Adam', 27));
customers.add(Customer('Katherin', 25));
customers.sortReversed();
print(customers);
}
出力:[{ Katherin, 25 }, { Jack, 23 }, { Adam, 27 }]
更なる読書
Reference
この問題について(オブジェクト降順の), 我々は、より多くの情報をここで見つけました https://dev.to/tienbku/dart-sort-list-of-objects-descending-57goテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol