Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

How to write do multifield sorting easily

Tue Oct 21, 2008 7:55 pm

I moved to Java from C++ as I love the language and its features
very much. However, there is a little problem I am facing while writing
comparison functions. In C++, it was easy to overload the < operator for
user defined class. But how to achieve the same elegantly in Java while
using custom classes? I will need to use these because of sorting vectors in
out-of-the-norm fashion.



Re: How to write do multifield sorting easily

Tue Oct 21, 2008 7:56 pm

Look at the Comparable<E> interface that defines the "int compareTo(E)"
method. This accomplishes the same thing as the six comparison operators
(although you can argue that C++'s operator==() is more like Java's
equals() method). How it works is you compare the result of that to
zero. Here are some examples:

// C++
std::string str1("a");
std::string str2("b");
bool comparison = (str1 < str2);

// Java
String str1 = "a";
String str2 = "b";
boolean comparison = (str1.compareTo(str2) < 0);

Notice how the comparison operator is the same, and the strings appear
in the same order. Under the hood Java is performing an extra operation,
comparing the two strings and then comparing the result to zero. C++ is
performing one comparison, but hiding it behind a "primitive" operator
that really calls a function.

Throughout much of the Collections framework and elsewhere you will see
discussion about "ordering consistent with equals." It is possible that
object1.compareTo(object2) == 0, yet object1.equals(object2) is false.
BigDecimal is a perfect example: two decimals may be equivalent in value
yet not equal (2.0 and 2.00, for example). This is an extra layer of
stuff on top of what C++ has by default, and something you should keep
an eye on. It is neither better nor worse, just different, and something
that can cause subtle bugs if you are not used to it.

One last note: you are better off with a List than a Vector. C++ uses
std::vector as its basic dynamic array. Java uses it for the same
purpose, but it adds synchronization, which is slow. Prefer to use
ArrayList, which is not thread safe. If need be you can wrap it in a
synchronized facade (Collections.synchronizedList() I believe), or use
one of the concurrency framework lists that are copy-on-write and
implement the same List interface.

Another last note: iterators behave slightly differently in Java and
C++. While they solve similar problems, they do so differently. In
particular, there is no algorithms package that accepts iterators in
Java like there is in C++. You can do the same things, you just get
there a different way.

Re: How to write do multifield sorting easily

Sun Jan 04, 2009 11:02 am

there is 2 ways :
1-you can implement "Comparable "interface"
eg:
Code:
public class x implements Comparable{
      private String ix;
      public int compareTo(x o1) {
          if (this.ix == o1).ix)
              return 0;
          else if ((this.ix) > o1.ix)
              return 1;
          else
              return -1;
    }
}


---------------------------------------------
2-you can implement Comparator Interface
Code:
public class x implements Comparator {
   private String ix;
   public int compare(x obj1, x obj2) {
        int Comp = obj1.ix.compareTo(obj2.ix);
        return Comp;
  }

now if you call Collection.sort(vector); it will work
http://www.java2s.com/Code/Java/Collect ... arator.htm
i hope it is what you want

Re: How to write do multifield sorting easily

Sun Jan 04, 2009 12:27 pm

thank you for your addition :)

Re: How to write do multifield sorting easily

Tue Feb 03, 2009 4:03 pm

Also you can use Collections.sort() method with your Comparator.

Post a reply
  Related Posts  to : How to write do multifield sorting easily
 How to write a code for sorting array of 100 number in C++     -  
 Earn Money Easily With Work At Home Jobs     -  
 c++ alphabetical and numerical sorting program     -  
 Need Help with Java Array Sorting Logic     -  
 list insertion sorting code in c++     -  
 Quicksort implementation C++ Code-Integers-Sorting     -  
 Incomplete code for array sorting and merging     -  
 quicksort algorithm implementation java code- array sorting     -  
 balloon sort algorithm C++ implementation code-sorting array     -  
 Bubble Sort Algorithm Java Implementation Code-Sorting Array     -