1
14
15 package com.liferay.portlet.shopping.util.comparator;
16
17 import com.liferay.portal.kernel.util.OrderByComparator;
18 import com.liferay.portlet.shopping.model.ShoppingItem;
19
20
25 public class ItemNameComparator extends OrderByComparator {
26
27 public static String ORDER_BY_ASC = "categoryId ASC, name ASC";
28
29 public static String ORDER_BY_DESC = "categoryId DESC, name DESC";
30
31 public static String[] ORDER_BY_FIELDS = {"categoryId", "name"};
32
33 public ItemNameComparator() {
34 this(false);
35 }
36
37 public ItemNameComparator(boolean asc) {
38 _asc = asc;
39 }
40
41 public int compare(Object obj1, Object obj2) {
42 ShoppingItem item1 = (ShoppingItem)obj1;
43 ShoppingItem item2 = (ShoppingItem)obj2;
44
45 Long categoryId1 = new Long(item1.getCategoryId());
46 Long categoryId2 = new Long(item2.getCategoryId());
47
48 int value = categoryId1.compareTo(categoryId2);
49
50 if (value == 0) {
51 value = item1.getName().toLowerCase().compareTo(
52 item2.getName().toLowerCase());
53 }
54
55 if (_asc) {
56 return value;
57 }
58 else {
59 return -value;
60 }
61 }
62
63 public String getOrderBy() {
64 if (_asc) {
65 return ORDER_BY_ASC;
66 }
67 else {
68 return ORDER_BY_DESC;
69 }
70 }
71
72 public String[] getOrderByFields() {
73 return ORDER_BY_FIELDS;
74 }
75
76 public boolean isAscending() {
77 return _asc;
78 }
79
80 private boolean _asc;
81
82 }