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
2 answers

Using compareTo in an Enum

I have seen this code, an am unsure how it works with the compareTo. Could someone guide me through how it works? public enum Test { POTATO("Potato"), TOMATO("Tomato"), CARROT("Carrot") public String Name; private Test(String…
the133448
  • 75
  • 7
2
votes
0 answers

Shifting Parts of an Array and NullPointerException

This is a fragment of the overridden method add for ArrayList. My intent is that, to keep the list in alphabetical order(by comparing strings; compareTo is defined in a separate method), it will use compareTo determine what elements should be…
Brynhildr Xie
  • 115
  • 1
  • 3
  • 10
2
votes
2 answers

compareTo() method in java (Student ids)

Learning java and having trouble with the compareTo method. I tried google but it was not much help for what i need.What i need is // compareTo public int compareTo(Student other) // is defined in the Comparable Interface // and should compare…
dave
  • 71
  • 1
  • 8
2
votes
2 answers

Implementing WritableComparable for Hadoop

I have implemented WritableComparable for my map job and have passed three values to it. public class KeyCustom implementsw WritableComparable { private Text placeOfBirth; private Text country; private LongWritable age; …
user3690321
  • 65
  • 2
  • 8
2
votes
2 answers

Comparing of Date objects in Java

I recently observed an oddity, when comparing two Java Date objects with equals(). Please note that both, this.getDate() and other.getDate(), will return a Java Date object in my application. Code: logger.debug("Date 1: " +…
user1438038
  • 5,821
  • 6
  • 60
  • 94
2
votes
1 answer

Generic IComparable implementation issue in Mono

This code executes successfully in .NET 4.0 public void CompareTest() { var m1 = new Foo { Order = 1 }; var m2 = new Foo { Order = 2 }; var c1 = new Bar { Order = -1 }; var c2 = new Bar { Order = 3 }; …
vexe
  • 5,433
  • 12
  • 52
  • 81
2
votes
3 answers

Sort Superclass list of objects that are Subclasses of the Superclass

Suppose I have an abstract Superclass named that is defined like this: public abstract class Member implements Comparable And then I have two Subclasses of Member, defined like this: public class Coach extends Member public class Player…
Gustavo Amgarten
  • 396
  • 4
  • 20
2
votes
3 answers

Insert into LinkedList Alphabetically

I am attempting to insert people alphabetically into a linked list by lastName. I have one really weird issue. The code as you see it works fine, except the list is in reverse order. What I can not figure out is why when I use the line of code: …
Michael Miner
  • 964
  • 2
  • 17
  • 39
2
votes
3 answers

Using compareTo(String) get error says undefined for the type String

public node add(String newWord, int ln, node cur) { if (cur == null) { return new node(newWord,ln); } int result=newWord.compareTo(cur.word); //compareTo is underlined suggesting an error if ( result == 0)…
Gavin Z.
  • 423
  • 2
  • 6
  • 16
2
votes
5 answers

Java: Three strings, lexicographic order

beginner Java programmer here. I am trying to compare three strings to each other, and have the system spit out the second/middle word in lexicographic order. import java.util.*; public class Ordered2 { public static void main(String[] args) { …
TGautier
  • 47
  • 1
  • 1
  • 8
2
votes
1 answer

Using equals() in compareTo()?

Test code (just to comply SSCCE, obviously there's much better ways to rock your own case-insensitive data models) public class TestClass implements java.lang.Comparable { public String test; @Override public int…
h.j.k.
  • 1,357
  • 2
  • 20
  • 30
2
votes
3 answers

TreeSet and compareTo() method. Single sort or multiple sorts

Im currently working on a program, where im using a TreeSet to store unique keys. I'm using a TreeSet, because I want them sorted. As of now, I have been making a TreeSet object, and added the strings one-at-a-time, when needed. TreeSet set = new…
Daniel Mac
  • 400
  • 2
  • 14
2
votes
3 answers

String in Java that always returns a certain result from compareTo?

Is there a String x that will always return a negative number when x.compareTo(y) is called, for any String y? Similarly, is there an x that will always return a positive number? Thanks.
r123454321
  • 3,323
  • 11
  • 44
  • 63
2
votes
2 answers

Sorting Generic Lists with CompareTo

I am trying to figure out the correct way to sort a generic list of objects. A quick example of my data structure: // The base class public abstract class Item : IComparable { public enum Category { Hats, Shirts, ... } public Category…
Mike
  • 33
  • 3
2
votes
2 answers

How do I use Java's Comparable to compare Generic objects in a tree?

Right now I'm trying to do an assignment involving creating a heap that can receive any generic object, and the nodes can compare each other by implementing the Comparable interface. Problem is, I can't find a way to compare generic object like…
user2309750
  • 1,403
  • 4
  • 14
  • 11