asp.net - IComparer on multiple values and specific values -
there 2 generic list of inspectors , inspectorrates. inspector class has object of inspector rate class.
ratetype has 3 different values (0 = not select ,1 = day rate ,2 = hourly rates).
i want show inspectors day type rate first , lowest rate. if user selects alternative "hourly rates" list needs sorted hourly rate , thenlowest rate. not seleted rate @ bottom.
example: after sorting, list content needs in next order (first 1 byday , sec hour)
i have create icomparer class sorting can't figure out how sort list based on specific , multiple fields.
public class inspectorsort implements icomparer(of inspector) private listorderby orderby = orderby.firstname private listdirection direction = direction.assending public enum orderby byte inspectorid = 0 firstname = 2 dayrate = 14 hourlyrate = 15 end enum public enum direction integer assending = 1 decending = -1 end enum public sub new(byval morderby orderby, byval mdirection direction) listorderby = morderby listdirection = mdirection end sub public function compare(byval x objects.inspector, byval y objects.inspector) integer implements system.collections.generic.icomparer(of objects.inspector).compare dim value integer = 0 select case listorderby case orderby.inspectorid if x.inspectorid > y.inspectorid value = 1 elseif x.inspectorid < y.inspectorid value = -1 end if case orderby.firstname if x.firstname.tolower > y.firstname.tolower value = 1 elseif x.firstname.tolower < y.firstname.tolower value = -1 end if case orderby.dayrate if x.defaultrate.ratetype = rates.ratetypeenum.day_rate if x.defaultrate.ratetype = rates.ratetypeenum.day_rate , x.defaultrate.rate > y.defaultrate.rate value = 1 end if end select homecoming value * listdirection end function end class
i didn't take time translate vb, based on c# comparer have created.
updated code
original
// ratetype 0 greatest of list if (x.defaultrate.ratetype == 0) homecoming 1;
this didn't factor in it's y has value want. added check y , homecoming value accordingly
so becomes
// ratetype 0 greatest of list if (x.defaultrate.ratetype == 0) homecoming 1; else if (y.defaultrate.ratetype == 0) homecoming -1; public class inspectordayratecomparer : icomparer<inspector> { public int compare(inspector x, inspector y) { // ratetype 0 greatest of list if (x.defaultrate.ratetype == 0) homecoming 1; else if (y.defaultrate.ratetype == 0) homecoming -1; int result = x.defaultrate.ratetype.compareto(y.defaultrate.ratetype); if (result == 0) result = x.defaultrate.rate.compareto(y.defaultrate.rate); homecoming result; } } public class inspectorhourlyratecomparer : icomparer<inspector> { public int compare(inspector x, inspector y) { // ratetype 0 greatest of list if (x.defaultrate.ratetype == 0) homecoming 1; else if (y.defaultrate.ratetype == 0) homecoming -1; // 1 greater 2 order desired int result = (x.defaultrate.ratetype.compareto(y.defaultrate.ratetype) *-1) ; if (result == 0) result = x.defaultrate.rate.compareto(y.defaultrate.rate; } }
this should closer want get.
icomparer interface here
if have more element add together compare, chain them using result variable, result == 0 means values same, must check other property comparer.
forcing homecoming value, when have no ratetype, create last.
asp.net vb.net icomparable
No comments:
Post a Comment