オブジェクト降順の


このチュートリアルでは、DART/Flutterの下にあるオブジェクトのリストを並べ替える方法と機能を紹介します
昇順に似ていますが、次のようにします.
  • カスタム比較機能を使用する.
  • 延長Comparable 抽象クラスcompareTo() 方法及び使用reversed .
  • 延長Comparable 抽象クラスcompareTo() 方法及び使用fast_immutable_collections 図書館.
  • ポスト:Dart/Flutter Sort List of objects

    カスタム比較関数を使用しているオブジェクトの


    カスタムの関数をリストに渡す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 }]
    

    更なる読書

  • 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