Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
Algorithm 通用排序函数接受T,但希望确保T具有可比性_Algorithm_Sorting_Generics_Dart - Fatal编程技术网

Algorithm 通用排序函数接受T,但希望确保T具有可比性

Algorithm 通用排序函数接受T,但希望确保T具有可比性,algorithm,sorting,generics,dart,Algorithm,Sorting,Generics,Dart,我在Dart中做一些简单合并排序的泛化 作为一个占位符,我认为节点的列表将成为列表的一个足够的包装器。因为T是一个对象,它本来就没有compareTo,我想你要找的是 class Node<T extends Comparable> 类节点 及 类合并排序{ 但是Comparable没有实现/谢谢,我会检查一下,鉴于t可以是字符串、num或对象,这是一种很好的方法吗?我正试图概括比较工具,因为我想将t与其他t进行比较。我想使用Comparable会是一个好主意。你可以这样做't

我在Dart中做一些简单合并排序的泛化


作为一个占位符,我认为
节点的列表将成为
列表
的一个足够的包装器。因为T是一个对象,它本来就没有compareTo,我想你要找的是

class Node<T extends Comparable>
类节点

类合并排序{

但是
Comparable
没有实现
/
谢谢,我会检查一下,鉴于t可以是字符串、num或对象,这是一种很好的方法吗?我正试图概括比较工具,因为我想将t与其他t进行比较。我想使用
Comparable
会是一个好主意。你可以这样做't juse
/
0
,但这取决于您实际使用它的目的。对于排序,我肯定会选择
Comparable
。我想使用Comparable,numerics会自动解析为字符串,这对于,==之类的东西可能不太合适,所以我可能需要深入研究分辨率再高一点?可能因为T扩展了comparable,这意味着T将有一个compareTo,它是基于T定义的…,所以它应该是好的,对吧?
类节点是您想要的。
class Node<T> extends Comparable{
  T _value;
  Node(T item){
    _value = item;
  }

  T getValue () => _value;

  bool operator ==(other) => identical(this, other);
  bool operator <( other){
    if (other is! T){
      return false;
    }

    //other is of same type, T.
    if (_value < (other as Node<T>).getValue()){
      return true;
    }
    return false;
  }
  bool operator <= (other){
    return (this == other) || (this < other);
  }

  int compareTo (other){
    if (this == other){ 
      return 0;  
    }
    if (this < other) { 
      return -1; 
    }
    return 1;  
  }
}
class Node<T extends Comparable>
class MergeSort<T extends Comparable>{