Questions tagged [compareto]

The CompareTo method is found in the .NET Framework and Java and imposes the natural ordering of a class. This is done by returning a negative integer, zero, or a positive integer as the instance of the implementing class is less than, equal to, or greater than the object it is compared to.

The CompareTo method is found in the .NET Framework and Java and imposes a natural ordering of a class. This is done by returning a negative integer, zero, or a positive integer as the instance of the implementing class is less than, equal to, or greater than the object it is compared to.

727 questions
2
votes
4 answers

Java Comparable to Instance or String

I'm trying to create a class that can either be compared to an instance of the same class, or to String. For example, consider the following: public class Record implements Comparable { public String name; public Record(String name)…
crush
  • 16,713
  • 9
  • 59
  • 100
2
votes
2 answers

IComparable.CompareTo compares with null in Sort

I want to sort a list with the help of IComparable.CompareTo for a type T called Path. I wrote var shortest = new List(); //Fill shortest with elements != null if (shortest.Contains(null)) throw new System.Exception("Path is…
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
2
votes
5 answers

How can I sort a string with integers in Java?

I have got an array. Each space in my array holds two strings (one string contains just letters, the other one numbers). What I am trying to do now is to sort the array either alphabetically or numerically (depending on which space in the array is…
user1420042
  • 1,689
  • 9
  • 35
  • 53
2
votes
4 answers

Sorting a list with two parameters using CompareTo

I am presently sorting a C# list using the 'CompareTo' method in the object type contained in the list. I want to sort ascendingly all items by their WBS (Work Breakdown Structure) and I can manage this very well using the following code: …
Jean-François Beaulieu
  • 4,305
  • 22
  • 74
  • 107
2
votes
4 answers

Using a comparator and a separate compareTo method for a PriorityQueue

If I have a PriorityQueue made up of T objects, and T has a compareTo() method and implements comparable, but my PriorityQueue also takes a comparator as a parameter, what is my PriorityQueue going to look to for the ordering of its elements? In…
nobillygreen
  • 1,548
  • 5
  • 19
  • 27
2
votes
6 answers

primitive type double error

sorry if my question looks very stupid. I get error on .compareTo() Cannot invoke compareTo(double) on the primitive type double! how can i fix this ? Thank you! Vehicle class: public class Vehicle implements IOutput { private double cost;} public…
SamR
  • 517
  • 3
  • 10
  • 24
2
votes
3 answers

Sorting students by name and score using an array

I posted a question about this last night but am still struggling. I need to sort a list of students from a text file first by name (in this format last name, first name), and then by test scores. I won't know how many students will be in the text…
newB
  • 35
  • 2
  • 2
  • 7
2
votes
2 answers

BigDecimal compareTo() thread safety

I have a Java application that uses the compareTo() method of the BigDecimal class in order to classify a (big) real number, read as a string, according to its type (basically, "too big", double or float). The application reads a very large number…
PNS
  • 19,295
  • 32
  • 96
  • 143
2
votes
3 answers

compareTo involving non-comparable field: how to maintain transitivity?

Consider a class with a comparable (consistent with equals) and a non-comparable field (of a class about which I do not know whether it overrides Object#equals or not). The class' instances shall be compared, where the resulting order shall be…
arne.b
  • 4,212
  • 2
  • 25
  • 44
2
votes
2 answers

compareTo() method java is acting weird

hi im having trouble getting this to work im getting an error here with my object comparison...how could I cast the inches to a string ( i never used compare to with anything other than strings) , or use comparison operators to compare the intigers,…
Ron Paul
  • 183
  • 3
  • 11
2
votes
1 answer

Hadoop Pig comparing two values and sort them

I'm currently learning the hadoop framework and the pig latin language. Now I've a problem. I've got a data-set with the following format: "long a, long b, char c, char d" Now I want to read this data-sets with pig. That's no problem with the load…
user1642208
2
votes
1 answer

VB.NET Sort Order Using CompareTo - Incorrect Results?

I have a long list of string values I am trying to sort in a grid. Initially, the default sort algorithm was used for the entire grid. However, I ended up having to rewrite the sorting for the grid because a few columns required special sorting and…
lmg
  • 43
  • 1
  • 7
2
votes
1 answer

Small issue with listbox.sort and string compareto-function

I'm having trouble with differences in the way a listbox sort its elements and the CompareTo-function. The thing is, I am using two listboxes, and trying to make two lists of elements only occurring in one of them. Both listboxes are sorted using…
Mads Aggerholm
  • 432
  • 1
  • 5
  • 13
2
votes
6 answers

java list of objects comparison

I am having List stud1 = new ArrayList(); and List stud2 = new ArrayList(); And Student class is having members like name, address. What i have to do is , I have to list the Student from stud1 (if stud1's Student…
Muthu
  • 1,550
  • 10
  • 36
  • 62
2
votes
2 answers

Need some assistance with my logic in cloning an object

Before I posted this, I read some of the previous posts and I really don't see anything wrong with my logic. (I'm 3 hours on this already and it's probably gonna kill my happy hour) *I never want to know the answers, I enjoy working hard so maybe if…
Addy75
  • 167
  • 2
  • 10