Generic Sorting function accepts T, but want to ensure T is comparable

Fallenreaper

I was doing some Genericizing of a simple MergeSort in Dart.

Just to be a placeholder, i thought a List of Node would make a sufficient wrapper to the List<T>. Since T is an object, it doesnt natively have compareTo, <, >, <= etc because it isnt a num or a string.

How do i get these warnings removed.

class Node<T> extends Comparable {
  T _value;
  Node(T value){
    _value = value;
  }
  //.....

}

class MergeSort<T>{
  list<Node<T>> _list;

  MergeSort(List<Node<T>> list){
    _list = list;

  }

  List<Node<T>> Sort( List<Node<T>> list ){
    _list = list;
    //.....
  }
}

The issue i am getting at is that in the MergeSort, i need to compare nodes, which is fine enough. I implement operator == etc to handle those cases, or operator < for those cases. I also have, since i extended Comparable, compareTo since Strings.

I was not sure how to accommodate for the classes being passed into Node, T, and i didn't know if there was a way to have it expect num, string, etc.

full class implementation + a shareable dartpad: https://dartpad.dartlang.org/645157fb547da482fc2b

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;  
  }
}

Maybe having a Node Wrapper is too much? I kinda feel like i might be able to strip away the Node class, and just have a List of T, but then the issue would be just pushed into the MergeSort when it comes to comparisons of the list elements.

Günter Zöchbauer

I think what you are looking for is

class Node<T extends Comparable>

and

class MergeSort<T extends Comparable>{

but Comparable doesn't implement the > / <. If you want to use these you can create your own superclass that does and require this class to be implemented instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Comparable<T> with Generic class

From Dev

implementing compareTo (Comparable<T>) for a Binary Tree(Generic)

From Dev

Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

From Dev

Generic type interface that accepts both types of Collection<T> and <T>

From Dev

Explanation of generic <T extends Comparable<? super T>> in collection.sort/ comparable code?

From Dev

Compare different subtypes of a generic type T with the Comparable interface

From Dev

Create a generic class with an array of type Comparable<T> in kotlin?

From Dev

Create a generic class with an array of type Comparable<T> in kotlin?

From Dev

Compare different subtypes of a generic type T with the Comparable interface

From Dev

Comparable<T> vs Raw Comparable

From Dev

Comparable<? super T> vs. Comparable<T>

From Dev

Generic Function: Cast T[] to Complex[]

From Dev

Comparable<T> and compareTo

From Dev

What isn't comparable by the Comparable class?

From Dev

Scala Generic Function... using T in Function

From Dev

Checking if non-member function that accepts T param exists

From Dev

Passing Data into function that accepts generic collection

From Dev

How can ensure that a function only accepts a positive integer?

From Dev

How can ensure that a function only accepts a positive integer?

From Dev

Should 'Comparable<T>' be a 'Functional interface'?

From Dev

How to convert a Comparable[] to a List<T>?

From Dev

Trouble Understanding Comparable<T> cast

From Dev

How to Return a Generic List (Of T) from a Function

From Dev

Delphi interface generic function returning TobjectList<T>

From Dev

Generic type <T> parameter BEFORE the function name

From Dev

Typescript Can't implement generic function interface

From Dev

Return HashSet<T> from HashSet of generic type in generic function

From Dev

What does <T extends Comparable<T>> mean?

From Dev

Issue with <T extends Comparable<? super T>>

Related Related

  1. 1

    Using Comparable<T> with Generic class

  2. 2

    implementing compareTo (Comparable<T>) for a Binary Tree(Generic)

  3. 3

    Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

  4. 4

    Generic type interface that accepts both types of Collection<T> and <T>

  5. 5

    Explanation of generic <T extends Comparable<? super T>> in collection.sort/ comparable code?

  6. 6

    Compare different subtypes of a generic type T with the Comparable interface

  7. 7

    Create a generic class with an array of type Comparable<T> in kotlin?

  8. 8

    Create a generic class with an array of type Comparable<T> in kotlin?

  9. 9

    Compare different subtypes of a generic type T with the Comparable interface

  10. 10

    Comparable<T> vs Raw Comparable

  11. 11

    Comparable<? super T> vs. Comparable<T>

  12. 12

    Generic Function: Cast T[] to Complex[]

  13. 13

    Comparable<T> and compareTo

  14. 14

    What isn't comparable by the Comparable class?

  15. 15

    Scala Generic Function... using T in Function

  16. 16

    Checking if non-member function that accepts T param exists

  17. 17

    Passing Data into function that accepts generic collection

  18. 18

    How can ensure that a function only accepts a positive integer?

  19. 19

    How can ensure that a function only accepts a positive integer?

  20. 20

    Should 'Comparable<T>' be a 'Functional interface'?

  21. 21

    How to convert a Comparable[] to a List<T>?

  22. 22

    Trouble Understanding Comparable<T> cast

  23. 23

    How to Return a Generic List (Of T) from a Function

  24. 24

    Delphi interface generic function returning TobjectList<T>

  25. 25

    Generic type <T> parameter BEFORE the function name

  26. 26

    Typescript Can't implement generic function interface

  27. 27

    Return HashSet<T> from HashSet of generic type in generic function

  28. 28

    What does <T extends Comparable<T>> mean?

  29. 29

    Issue with <T extends Comparable<? super T>>

HotTag

Archive