.net - Creating and sorting an array/arraylist/list -
currently, have list(of string) contains data similar to:
"207.5,1" "373,2" "278.5,3" "134,4" "277,5" "674,7" "58.5,9" to list, apply 2 commands 'list.sort' 'list.reverse' both expected, list contains:
"674,7" "58.5,9" "373,2" "278.5,3" "277,5" "207.5,1" "134,4" as can see, intents , purposes, has worked perfectly, but, sharp-eyed notice entry "58.5,9" out of place , should @ bottom of list.
i appreciate sorting strings here, i'm bound fail. need discover please, how can copy contents of strings per line sortable container, stores numbers , 'indexes' integers and/or singles? ideally, i'll end array or whatever of data this:
674.0,7 373.0,2 278.5,3 277.0,5 207.5,1 134.0,4 58.5,9 i have tried many iterations can think of (fairly new missing obvious!). please if can! thanks.
if you're going using data more once makes sense have class represents it. way can give parts meaningful names, have easy way create new item, manipulate data, , have own way of converting string.
it may load of fiddly code, have write once , life simpler when want use data:
option infer on option strict on module module1 public class datum property number decimal property index integer sub new() ' default constructor end sub sub new(numberindex string) dim parts = numberindex.split(","c) ' simple parameter check if parts.length <> 2 throw new argumentexception("no comma found in " & nameof(numberindex)) end if number = cdec(parts(0)) index = cint(parts(1)) end sub public overrides function tostring() string return $"{number},{index}" end function end class sub main() dim mylist new list(of string) {"207.5,1", "373,2", "278.5,3", "134,4", "277,5", "674,7", "58.5,9"} dim mydata = (mylist.select(function(d) new datum(d))).tolist() dim datadescending = mydata.orderbydescending(function(d) d.number).tolist() console.writeline(string.join(vbcrlf, datadescending)) console.readline() end sub end module outputs:
674,7 373,2 278.5,3 277,5 207.5,1 134,4 58.5,9
Comments
Post a Comment