c# - Why when I sort a list full of 0's it gets mixed up? -
i'm trying rate gin's popularity, read in gin list text file , each gin starts popularity of 0, when sort list comes out mixed, not in it's original alphabetical order if not sorted.
no sorted -
here code i'm using -
using unityengine; using system.collections; using system.collections.generic; public class ginlistclass : monobehaviour { [header("the text file holding gin's, country, abv , price")] public textasset gintextfile; [header("the lists, public debugging")] public list< string > ginstring = new list< string >(); private int rating; private int linesinfilecount; void start () { rating = 0; list<ginlist> gins = new list<ginlist>(); string[] linesinfile = gintextfile.text.split('\n'); foreach (string line in linesinfile) { linesinfilecount++; ginstring.add(line); } (var = 0; < linesinfilecount; i++) { gins.add( new ginlist(ginstring[i], ginstring[i+1], ginstring[i+2], ginstring[i+3], rating+random.range(0,100))); i+=4; } gins.sort(); //gins.reverse(); debug.log("a .. gin count " + gins.count); foreach(ginlist gin in gins) { debug.log(gin._ginname + " - " + gin._country + " - " + gin._abv + " - " + gin._price + " - " + gin._pop); } gins.clear(); }
}
and -
using unityengine; using system.collections; using system.collections.generic; public class ginlistclass : monobehaviour { [header("the text file holding gin's, country, abv , price")] public textasset gintextfile; [header("the lists, public debugging")] public list< string > ginstring = new list< string >(); private int linesinfilecount; void start () { list<ginlist> gins = new list<ginlist>(); string[] linesinfile = gintextfile.text.split('\n'); foreach (string line in linesinfile) { linesinfilecount++; ginstring.add(line); } (var = 0; < linesinfilecount; i++) { gins.add( new ginlist(ginstring[i], ginstring[i+1], ginstring[i+2], ginstring[i+3], random.range(0,100))); i+=4; } //gins.sort(); //gins.reverse(); debug.log("a .. gin count " + gins.count); foreach(ginlist gin in gins) { debug.log(gin._ginname + " - " + gin._country + " - " + gin._abv + " - " + gin._price + " - " + gin._pop); } gins.clear(); }
}
if give gin's rating sort correctly.
can pleas point me in right direction, thank you.
some sort algorithms stable, not. probably, sort algorithm used library not stable, i.e., may reorder items compare equally.
as solution, order first popularity, if have same popularity, sort name. don't know c# (or whatever using) should pseudo-code
int compare(const myobject & i1, const myobject & i2) { if(compare(i1.popularity, i2.popularity) == 0) { // reverse sort popularity return -compare(i1.popularity, i2.popularity); } return compare(i1.name, i2.set2id); }
use as
gins.sort(compare);
edit: solution
Comments
Post a Comment