c# - Sorting out a vector by smallest number first, doing the exact same opposite? -
i've been trying make vector/object sorter day , if exchange signs (< , >) nothing seems happening. have integers randomly put in random order , objective program sort them size. have sketched out on paper better overview when try sort out lowest highest in descending order, seems highest lowest. hope swedish in code won't disturb. appreciated. thank you.
(int = 0; < 25; i++) { (int = i; < 25; a++) { int ettan = price[i]; int tvåan = price[a]; if (ettan > tvåan) { dryck tmp = new dryck(null, 0, null); tmp = drycker[i]; drycker[i] = drycker[a]; drycker[a] = tmp; continue; } } } edit: forgot add, it's supposed compare first integer [0] [1] [2] , on, until has found 1 lower [i], , exchange them eachother, lower integer placed in first spot etc.
at first vectors in c# called arrays ;-)
in nutshell comparing products wrong prices. why? let me show example:
there 2 important arrays:
drycker: prices: cola -> 25 fanta -> 20 sprite -> 30 when compare cola fanta new lists like:
drycker: prices: fanta -> 25 -> price fanta 25 cola -> 20 sprite -> 30 the point comparing products positions , change product, not price. answer: change prices, too, in order keep arrays consistent.
other suggestions:
maybe 1 array (drycker) enough (depends on code). compare directly:
if( ((dryck) drycker[i]).price > ((dryck) drycker[a]).price) or use linq list:
list<dryck> drycker = new list<dryck>(); //strongly typed lists recommended drycker.sort(x => x.price); the last suggestions contains new stuff you. search list, linq , lambda expressions go little deeper. "high quality" solution :)
Comments
Post a Comment