Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vector sorting

Status
Not open for further replies.

timmbo

Programmer
Joined
Feb 22, 2001
Messages
167
Location
US
Hi All,

I need to sort my Vector by amount in Descending order. How would I go about this? I have account sorting Asc, but within account the amount has to be in descending order. Any help would be most appreciated.

Code:
public void AmountSorter(Vector _wireRecords) {
   LogManager.sysLog(5, this, "Entering RecordSorter Constructor()");

   String oldAcct = new String();
   Vector holdInfo = new Vector(); 

   for(int i = 0; _wireRecords.size() > i; i++) {
      WireRecord wr = (WireRecord)_wireRecords.elementAt(i);
      holdInfo.addElement(wr.getAccountNum());
      holdInfo.addElement(wr.getAmount()); 
   }
   System.out.println("info... " + holdInfo);

   LogManager.sysLog(5, this, "Exiting RecordSorter Constructor()");
}//End of Method AmountSorter...

TIA,
Tim
 
The java.util.Collections class has static methods to sort your Vector (remember that Vector implements List):


Example code:
Code:
import java.util.*;

class Custom {
  int i;
  String s;
}

public class Sorter {

  private static class IntComparator implements Comparator {
    public int compare(Object o1, Object o2) {
      int i1 = ((Custom)o1).i;
      int i2 = ((Custom)o2).i;
      return i1 - i2;
    }
  }

  private static class StringComparator implements Comparator {
    public int compare(Object o1, Object o2) {
      String s1 = ((Custom)o1).s;
      String s2 = ((Custom)o2).s;
      return s1.compareTo(s2);
    }
  }

  public static void integerSort(List l) {
    Collections.sort(l, new IntComparator());
  }
  public static void stringSort(List l) {
    Collections.sort(l, new StringComparator());
  }

  public static void main(String[] args) {
    Vector v = new Vector();
    Custom a = new Custom();
    Custom b = new Custom();
    a.i = 1; a.s = "A";
    b.i = -1; b.s = "B";
    v.add(b);
    v.add(a);
    integerSort(v);
    Iterator iterator = v.iterator();
    while(iterator.hasNext())
      System.out.println( ((Custom)iterator.next()).i );
    stringSort(v);
    iterator = v.iterator();
    while(iterator.hasNext())
      System.out.println( ((Custom)iterator.next()).s );
  }
}

Hope this helps. Cheers, Neil
 
And a slightly more efficient implementation:
Code:
  private static final Comparator
    BY_INTS = new Comparator() {
      public int compare(Object o1, Object o2) {
        int i1 = ((Custom)o1).i;
        int i2 = ((Custom)o2).i;
        return i1 - i2;
      }
    },
    BY_STRINGS = new Comparator() {
      public int compare(Object o1, Object o2) {
        String s1 = ((Custom)o1).s;
        String s2 = ((Custom)o2).s;
        return s1.compareTo(s2);
      }
    };

  public static void integerSort(List l) {
    Collections.sort(l, BY_INTS);
  }
  public static void stringSort(List l) {
    Collections.sort(l, BY_STRINGS);
  }
Cheers, Neil
 
Thanks All for your responses. I'll check them out.

Tim
 
Thanks again Neil for your response. With me being relitively new to Java, how do I encorporate this into my routine. How would I get my information to these methods?

TIA,
Tim
 
I would rewrite your code from:
Code:
public void AmountSorter(Vector _wireRecords) {
   LogManager.sysLog(5, this, "Entering RecordSorter Constructor()");

   String oldAcct = new String();
   Vector holdInfo = new Vector(); 

   for(int i = 0; _wireRecords.size() > i; i++) {
      WireRecord wr = (WireRecord)_wireRecords.elementAt(i);
      holdInfo.addElement(wr.getAccountNum());
      holdInfo.addElement(wr.getAmount()); 
   }
   System.out.println("info... " + holdInfo);

   LogManager.sysLog(5, this, "Exiting RecordSorter Constructor()");
}//End of Method AmountSorter...
To:
Code:
public void AmountSorter(Vector _wireRecords) {

  // This method sorts the incoming Vector.
  // It assumes that each element in the Vector
  // is an instance of WireRecord.

  // It also assumes that the method getAmount() returns
  // a float (I can't tell from the code fragment posted).

  Collections.sort( _wireRecords,
    new Comparator() {
      public int compare(Object o1, Object o2) {
        WireRecord wr1 = (WireRecord)o1;
        WireRecord wr2 = (WireRecord)o2;
        return Float.compare(
          wr1.getAmount(),
          wr2.getAmount()
        );
      }
    } );
}//End of Method AmountSorter...
Cheers, Neil
 
Thanks Neil.

getAmount() returns a double. I changed float to double and got the following error.

Can't invoke a method on a double.
return double.compare(


I am assuming I would have to convert to a float.

I only need to sort on the amount. Will this mess up the order of accounts?

TIA,
Tim
 
My bad Neil,

I changed "Float" to "Double" and received the following error.


Method compare(java.lang.Double, java.lang.Double) not found in class java.lang.Double.
return Double.compare(



TIA,
Tim
 
Looks like your getAmount is returning a Double, not a double. In which case, add the following lines:
Code:
Double d1 = wr1.getAmount();
Double d2 = wr2.getAmount();
return d1.compareTo(d2);
A Double is an object instance that wraps a double primitive, for example [tt]Double d = new Double(1.0d)[/tt]
Hope this helps. Cheers, Neil

 
Thanks for all your time and help Neil.
 
I am screwing this thing three ways from Sunday. Do I need to replace

return Double.compare(
wr1.getAmount(),
wr2.getAmount()

with your code? My apolgies on being "thick" on this one.

Code:
public void AmountSorter(Vector _wireRecords) {
   Collections.sort(_wireRecordso, new Comparator() {
      public double compare(Object o1, Object o2) {
      WireRecord wr1 = (WireRecord)o1;
      WireRecord wr2 = (WireRecord)o2;
      Double d1 = wr1.getAmount();
      Double d2 = wr2.getAmount();
      return d1.compareTo(d2);

      );
    }
} );
}//End of Method AmountSorter...
 
Here is the latest code...

Code:
public void AmountSorter(Vector _wireRecords) {
   Collections.sort(_wireRecords, new Comparator() {
     public Double compare(Object o1, Object o2) {
        WireRecord wr1 = (WireRecord)d1;
        WireRecord wr2 = (WireRecord)d2;
        Double d1 = wr1.getAmount();
        Double d2 = wr2.getAmount();
        return d1.compareTo(d2);
     );
   }
} );
}//End of Method AmountSorter...

I am rec. the following errors.

';' expected. return d1.compareTo(d2);

The method java.lang.Double compare(java.lang.Object, java.lang.Object) declared in local class com.pnc.tms.wire.wirerecord.Records. 1 cannot override the method of the same signature declared in interface java.util.Comparator. They must have the same return type. public Double compare(Object o1, Object o2) {


Any suggestions would be most appreciated....

Tim
 
Think I got most of the problems worked out.

Code:
public void AmountSorter(Vector _wireRecords) {
   Collections.sort(_wireRecords, new Comparator() {
      public int compare(Object o1, Object o2) {
         WireRecord wr1 = (WireRecord)o1;
	 WireRecord wr2 = (WireRecord)o2;
	 Double d1 = wr1.getAmount();
	 Double d2 = wr2.getAmount();
      }
    } );
//End of Method AmountSorter...

I'm missing the "return" part now.

T
 
The compilor is asking for "}" after "Double" at d1 = wr1.getAmount();

Code:
public void AmountSorter(Vector _wireRecords) {
   Collections.sort(_wireRecords, new Comparator() {
      public int compare(Object o1, Object o2) {
         WireRecord wr1 = (WireRecord)o1;
         WireRecord wr2 = (WireRecord)o2;
	 return Double.compare(
	    Double d1 = wr1.getAmount();
	    Double d2 = wr2.getAmount();
         );
      };
   } );
}//End of Method AmountSorter...

Any thoughts...

T
 
Hi All,

I was reading through this thread and had a few questions.

Timbo: In this statement: WireRecord wr = (WireRecord)_ wireRecords.elementAt(i);

Is WireRecord a class you defined or a class in Java. I did a search in Java but I could not find it.


Toolkit:

private static final Comparator
BY_INTS = new Comparator() {
public int compare(Object o1, Object o2) {
int i1 = ((Custom)o1).i;
int i2 = ((Custom)o2).i;
return i1 - i2;
}
},
BY_STRINGS = new Comparator() {
public int compare(Object o1, Object o2) {
String s1 = ((Custom)o1).s;
String s2 = ((Custom)o2).s;
return s1.compareTo(s2);
}
};

Could you elaborate on what BY_INTS and BY_STRINGS mean. I'm new to Java and a little confused on what is taking place here. Are you using a Comparator constructor here?


Soutine





 
The idea is to sort the incoming Vector. The Collections class provides static methods to sort a List (and Vector implements List). Sorting methods rely on a comparing two elements in the List, and saying which element should appear before the other. This is provided by creating an implementation of the Comparator interface, by overriding the following method:
Code:
public int compare(Object o1, Object o2)
When the return value is less than zero, o1 is considered 'less than' o2; when the return value is greater than zero, o2 is considered 'greater than' o2; and a return value of zero indicates that o1 and o2 are considered 'equal' in terms of the position in a sorted list. In the example code above, we define two class level Comparator reference variables: BY_INTS and BY_STRINGS. In fact, the BY_INTS reference points to an anonymous implementation of the Comparator interface, where the compare method has been overridden to provide sorting based on a primitive int member variable. Similarly the BY_STRINGS reference points to another anonymous implementation of the Comparator interface, where the compare method has been overridden to provide sorting based on a String member variable.
Hope this hasn't confused you more ;-)
 
Hi Toolkit,

Thanks. I don't think of it as being confused. It's just more information to read about and ask questins about.

Soutine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top