# Sort an Array<String> alphabetically

7 messages
Open this post in threaded view
|

## Sort an Array<String> alphabetically

 Hello list.I'd like to sort a simple array :-- var a = ["b", "a", "ce", "ad"];alphabetically.So, it would be ["a","ad","b","ce"] It's pretty simple, but I can't find a way to do that in haXe : array have a sort method, but I can't understand how it works.ThanksBlue112 -- haXe - an open source web programming language http://haxe.org
Open this post in threaded view
|

## Re: Sort an Array<String> alphabetically

 There are sure to be better ways but this is how I did it:var desc = true;array.sort (lowestString); function lowestString (s1 : String, s2 : String) : Int { if (s1 == s2) return 0; s1 = s1.toLowerCase (); s2 = s2.toLowerCase (); for (i in 0...s1.length) { var n1 : Int = s1.charCodeAt (i); var n2 : Int = s2.charCodeAt (i); if (n1 < n2) return (desc ? 1 : -1); // If descending, the other way around else if (n2 < n1) return (desc ? -1 : 1); } return (s1.length < s2.length ? (desc ? 1 : -1) : (desc ? -1 : 1)); }On Thu, May 28, 2009 at 12:39 AM, Blue Sans douze wrote: Hello list.I'd like to sort a simple array :-- var a = ["b", "a", "ce", "ad"]; alphabetically.So, it would be ["a","ad","b","ce"] It's pretty simple, but I can't find a way to do that in haXe : array have a sort method, but I can't understand how it works.ThanksBlue112 -- haXe - an open source web programming language http://haxe.org -- haXe - an open source web programming language http://haxe.org
Open this post in threaded view
|

## Re: Sort an Array<String> alphabetically

 Nice, even if it's pretty complex.It would be great if something like that would be integrated in haXe api (maybe in ArraySort class, or something like that ?).Well, thanks anyway :) 2009/5/28 Viktor Hesselbom There are sure to be better ways but this is how I did it:var desc = true;array.sort (lowestString); function lowestString (s1 : String, s2 : String) : Int { if (s1 == s2) return 0; s1 = s1.toLowerCase (); s2 = s2.toLowerCase (); for (i in 0...s1.length) { var n1 : Int = s1.charCodeAt (i); var n2 : Int = s2.charCodeAt (i); if (n1 < n2) return (desc ? 1 : -1); // If descending, the other way around else if (n2 < n1) return (desc ? -1 : 1); } return (s1.length < s2.length ? (desc ? 1 : -1) : (desc ? -1 : 1)); }On Thu, May 28, 2009 at 12:39 AM, Blue Sans douze wrote: Hello list.I'd like to sort a simple array : -- var a = ["b", "a", "ce", "ad"]; alphabetically.So, it would be ["a","ad","b","ce"] It's pretty simple, but I can't find a way to do that in haXe : array have a sort method, but I can't understand how it works.ThanksBlue112 -- haXe - an open source web programming language http://haxe.org -- haXe - an open source web programming language http://haxe.org -- haXe - an open source web programming language http://haxe.org
Open this post in threaded view
|

## Re: Sort an Array<String> alphabetically

 Madrok, on IRC, give me this nice function :     private function strSort(a:String, b:String):Int    {        a = a.toLowerCase();        b = b.toLowerCase();                if (a < b) return -1;         if (a > b) return 1;        return 0;    }It's kinda less complex and it works perfectly :) Toc.2009/5/28 Blue Sans douze Nice, even if it's pretty complex.It would be great if something like that would be integrated in haXe api (maybe in ArraySort class, or something like that ?).Well, thanks anyway :) 2009/5/28 Viktor Hesselbom There are sure to be better ways but this is how I did it:var desc = true;array.sort (lowestString); function lowestString (s1 : String, s2 : String) : Int { if (s1 == s2) return 0; s1 = s1.toLowerCase (); s2 = s2.toLowerCase (); for (i in 0...s1.length) { var n1 : Int = s1.charCodeAt (i); var n2 : Int = s2.charCodeAt (i); if (n1 < n2) return (desc ? 1 : -1); // If descending, the other way around else if (n2 < n1) return (desc ? -1 : 1); } return (s1.length < s2.length ? (desc ? 1 : -1) : (desc ? -1 : 1)); }On Thu, May 28, 2009 at 12:39 AM, Blue Sans douze wrote: Hello list.I'd like to sort a simple array : -- var a = ["b", "a", "ce", "ad"]; alphabetically.So, it would be ["a","ad","b","ce"] It's pretty simple, but I can't find a way to do that in haXe : array have a sort method, but I can't understand how it works.ThanksBlue112 -- haXe - an open source web programming language http://haxe.org -- haXe - an open source web programming language http://haxe.org -- haXe - an open source web programming language http://haxe.org
Open this post in threaded view
|

## Re: Sort an Array<String> alphabetically

 Oh, I didn't know you could do that. Great! :)On Thu, May 28, 2009 at 1:06 AM, Blue Sans douze wrote: Madrok, on IRC, give me this nice function :     private function strSort(a:String, b:String):Int    {        a = a.toLowerCase();         b = b.toLowerCase();                if (a < b) return -1;         if (a > b) return 1;        return 0;    }It's kinda less complex and it works perfectly :) Toc.2009/5/28 Blue Sans douze Nice, even if it's pretty complex.It would be great if something like that would be integrated in haXe api (maybe in ArraySort class, or something like that ?).Well, thanks anyway :) 2009/5/28 Viktor Hesselbom There are sure to be better ways but this is how I did it:var desc = true;array.sort (lowestString); function lowestString (s1 : String, s2 : String) : Int { if (s1 == s2) return 0; s1 = s1.toLowerCase (); s2 = s2.toLowerCase (); for (i in 0...s1.length) { var n1 : Int = s1.charCodeAt (i); var n2 : Int = s2.charCodeAt (i); if (n1 < n2) return (desc ? 1 : -1); // If descending, the other way around else if (n2 < n1) return (desc ? -1 : 1); } return (s1.length < s2.length ? (desc ? 1 : -1) : (desc ? -1 : 1)); }On Thu, May 28, 2009 at 12:39 AM, Blue Sans douze wrote: Hello list.I'd like to sort a simple array : -- var a = ["b", "a", "ce", "ad"]; alphabetically.So, it would be ["a","ad","b","ce"] It's pretty simple, but I can't find a way to do that in haXe : array have a sort method, but I can't understand how it works.ThanksBlue112 -- haXe - an open source web programming language http://haxe.org -- haXe - an open source web programming language http://haxe.org -- haXe - an open source web programming language http://haxe.org -- haXe - an open source web programming language http://haxe.org