Sort an Array<String> alphabetically

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Sort an Array<String> alphabetically

Blue Sans douze
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.

Thanks
Blue112

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Sort an Array<String> alphabetically

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 <[hidden email]> 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.

Thanks
Blue112

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Sort an Array<String> alphabetically

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 <[hidden email]>
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 <[hidden email]> 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.

Thanks
Blue112

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Sort an Array<String> alphabetically

Blue Sans douze
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 <[hidden email]>
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 <[hidden email]>

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 <[hidden email]> 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.

Thanks
Blue112

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Sort an Array<String> alphabetically

Viktor Hesselbom
Oh, I didn't know you could do that. Great! :)

On Thu, May 28, 2009 at 1:06 AM, Blue Sans douze <[hidden email]> 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 <[hidden email]>

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 <[hidden email]>

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 <[hidden email]> 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.

Thanks
Blue112

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Sort an Array<String> alphabetically

Nicolas Cannasse
>         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;
>         }

Even shorter :

myArray.sort(function(a,b)
  return Reflect.compare(a.toLowerCase(),b.toLowerCase())
);

Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Sort an Array<String> alphabetically

Hudson Ansley
a detailed explanation of how this works might make a great entry in
the docs :-)
On the subject of reflection, I think this is misunderstood and
important enough to warrant its own entry in the haXe Language
Reference page. Currently it only has an API entry with little general
information. Use-cases would be great, as well as when it is not good
to use and why (API just says "use with caution")
Regards,
Hudson

On Thu, May 28, 2009 at 4:40 AM, Nicolas Cannasse
<[hidden email]> wrote:
>
> myArray.sort(function(a,b)
>  return Reflect.compare(a.toLowerCase(),b.toLowerCase())
> );
>
> Nicolas
>

--
haXe - an open source web programming language
http://haxe.org