Repeat-Control: Sorting a vector in java

Repeat-Control: Sorting a vector in java

Use Case:

Companies need to be sorted by its sales order totals.
In this case i loop through all Orders and create an SalesOrderTotal Object for each Company. After that i got  a vector with SalesOrderTotal Objects, which are sorted by Company Name.
In order to have the Customer with the highest Order Total first, i need to resort the SalesOrderTotal Objects.

Step 1: Create the Vector with the SalesOrderTotal objects
Step 2: Create a Comparator Class (for example an Util Class with an embedded Class):

import java.util.Comparator;

public class Util {
	public static Comparator SalesOrderTotalValueComparator = new Comparator() {
		public int compare(Object salesOrderTotal, Object salesOrderTotal2) {
			Double dblAmount1 = ((SalesOrderTotal) salesOrderTotal).getAmount();
			Double dblAmount2 = ((SalesOrderTotal)salesOrderTotal2).getAmount();
			return dblAmount2.compareTo(dblAmount1);

Step 3: Sort the Vector (for example in a “refreshCompanySalesOrders” Java Method):

Collections.sort(vSalesOrderTotals, Util.SalesOrderTotalValueComparator);

 Step 4: Create a Repeat Control to display the objects:

Sorted Java Objects by value

Sorted Java Objects by value

Leave a Reply