c# - Sorting array by item type -
i have array of objects sort type. within array set of objects of 1 type in preferred relative order. after sort, these objects grouped together, no longer in same order. using sort follows
array.sort(shapes, getvisualcomparer());
i have looked sort method promises preserve original order, have found nothing.
i note have workaround, adds unnecessary confusion code, , not address general problem if comes in future.
make copy of original array. then, make comparer compare original indexes of elements if type equal.
i don't know on base type sorting, sorting name, this:
public class typecomparer : icomparer<type> { public int compare(type x, type y) { int result = stringcomparer.invariantculture.compare(x.name, y.name); if (result == 0) { result = array.indexof(originalarray, x).compareto(array.indexof(originalarray, y)); } return result; } }
Comments
Post a Comment