Jump to page: 1 2
Thread overview
Sorting an Array
Jul 10, 2007
okibi
Jul 10, 2007
Sean Kelly
Jul 10, 2007
gogamza
Jul 10, 2007
Frits van Bommel
Jul 10, 2007
downs
Jul 10, 2007
okibi
Jul 10, 2007
Regan Heath
Jul 10, 2007
okibi
Jul 11, 2007
Regan Heath
Jul 10, 2007
torhu
Jul 11, 2007
torhu
Jul 11, 2007
Oskar Linde
Jul 10, 2007
okibi
Jul 10, 2007
torhu
Jul 10, 2007
okibi
Jul 11, 2007
downs
Jul 11, 2007
downs
Jul 11, 2007
okibi
July 10, 2007
Is there an easy way to sort an array with elements such as "12 - Some text" or "241 - Other Text"?

I want to sort in order of the numbers. Is this possible?

Thanks!
July 10, 2007
okibi wrote:
> Is there an easy way to sort an array with elements such as "12 - Some text" or "241 - Other Text"?
> 
> I want to sort in order of the numbers. Is this possible?

You need a sort routine with a custom comparator.  Tango has one in tango.core.Array.  Unfortunately, I don't think there is such a routine in Phobos.


Sean
July 10, 2007
okibi Wrote:

> Is there an easy way to sort an array with elements such as "12 - Some text" or "241 - Other Text"?
> 
> I want to sort in order of the numbers. Is this possible?
> 
> Thanks!


how about this method?


---------------------------------------------------------------------------------------------------
import std.stdio;
import std.string;
import std.conv;

struct keyvalue{
	char[] str;

	int opCmp(keyvalue* s){
		char[][] paramKV = split(s.str, "-");
		char[][] thisKV = split(this.str, "-");
		return toInt(thisKV[0]) - toInt(paramKV[0]);
	}
}



void main(){
	static keyvalue keyval = {"12-Some text"};
	static keyvalue keyval2 = {"6-Other Text"};
	static keyvalue keyval3 = {"7-Other Text2"};

	keyvalue[] keyvals = [keyval, keyval2, keyval3];

	keyvalue[] keyvals2 = keyvals.sort;

	foreach(keyvalue kv;keyvals2){
		writefln(kv.str);
	}
}

----------------------------------------------------------------------------------------------------


July 10, 2007
gogamza wrote:
>     int opCmp(keyvalue* s){
>         char[][] paramKV = split(s.str, "-");
>         char[][] thisKV = split(this.str, "-");
>         return toInt(thisKV[0]) - toInt(paramKV[0]);
>     }

That'll start going wrong once the difference between the integers starts to get over int.max. The safest way to return an opCmp value for integers and larger basic types is with things like:
---
auto val1 = toInt(thisKV[0]);
auto val2 = toInt(paramVK[0]);
return typeid(int).compare(&val1, &val2);
---
(Optionally, replace 'int' with 'typeof(val1)' in that last line if you're not sure the type returned by toInt will always be an int)
July 10, 2007
okibi wrote:
> Is there an easy way to sort an array with elements such as "12 - Some text" or "241 - Other Text"?
> 
> I want to sort in order of the numbers. Is this possible?
> 
> Thanks!
// Sort array via comparison operator.
// 'bigger' returns if 'value' is bigger than 'than'.
void sort(T)(ref T[] array, bool function(T value, T than) bigger) {
  // comparing sorting algorithm goes here. See wikipedia for examples.
  // Quicksort should do nicely and is easy to implement.
}

long getNumericPart(char[] inp) {
  if (!input.length) return long.max+1; // no number is very small.
  size_t numlen=0;
  while (numlen<inp.length)&&(inp[numlen]>='0')&&(inp[numlen]<='9'))
    ++numlen;
  return inp[0..numlen];
}

void main() {
  char[][] array=getArray(); // array is read
  array.sort(function(char[] a, char[] b) {
    return getNumericPart(a)>getNumericPart(b);
  });
}

Didn't try, obviously, but it should work.
Have fun! :D
 --downs
July 10, 2007
This does seem like the best way, but I'm not sure how I would go about implement Quicksort into that code. Could you please give me some pointers?

Thanks!

downs Wrote:

> okibi wrote:
> > Is there an easy way to sort an array with elements such as "12 - Some text" or "241 - Other Text"?
> > 
> > I want to sort in order of the numbers. Is this possible?
> > 
> > Thanks!
> // Sort array via comparison operator.
> // 'bigger' returns if 'value' is bigger than 'than'.
> void sort(T)(ref T[] array, bool function(T value, T than) bigger) {
>    // comparing sorting algorithm goes here. See wikipedia for examples.
>    // Quicksort should do nicely and is easy to implement.
> }
> 
> long getNumericPart(char[] inp) {
>    if (!input.length) return long.max+1; // no number is very small.
>    size_t numlen=0;
>    while (numlen<inp.length)&&(inp[numlen]>='0')&&(inp[numlen]<='9'))
>      ++numlen;
>    return inp[0..numlen];
> }
> 
> void main() {
>    char[][] array=getArray(); // array is read
>    array.sort(function(char[] a, char[] b) {
>      return getNumericPart(a)>getNumericPart(b);
>    });
> }
> 
> Didn't try, obviously, but it should work.
> Have fun! :D
>   --downs

July 10, 2007
I believe the array.sort routine uses Quicksort.  So, the code below already implements it.

Regan

okibi wrote:
> This does seem like the best way, but I'm not sure how I would go about implement Quicksort into that code. Could you please give me some pointers?
> 
> Thanks!
> 
> downs Wrote:
> 
>> okibi wrote:
>>> Is there an easy way to sort an array with elements such as "12 - Some text" or "241 - Other Text"?
>>>
>>> I want to sort in order of the numbers. Is this possible?
>>>
>>> Thanks!
>> // Sort array via comparison operator.
>> // 'bigger' returns if 'value' is bigger than 'than'.
>> void sort(T)(ref T[] array, bool function(T value, T than) bigger) {
>>    // comparing sorting algorithm goes here. See wikipedia for examples.
>>    // Quicksort should do nicely and is easy to implement.
>> }
>>
>> long getNumericPart(char[] inp) {
>>    if (!input.length) return long.max+1; // no number is very small.
>>    size_t numlen=0;
>>    while (numlen<inp.length)&&(inp[numlen]>='0')&&(inp[numlen]<='9'))
>>      ++numlen;
>>    return inp[0..numlen];
>> }
>>
>> void main() {
>>    char[][] array=getArray(); // array is read
>>    array.sort(function(char[] a, char[] b) {
>>      return getNumericPart(a)>getNumericPart(b);
>>    });
>> }
>>
>> Didn't try, obviously, but it should work.
>> Have fun! :D
>>   --downs
> 
July 10, 2007
Well, the following comment made it seem like it won't work:

>>    // comparing sorting algorithm goes here. See wikipedia for examples.
>>    // Quicksort should do nicely and is easy to implement.


Regan Heath Wrote:

> I believe the array.sort routine uses Quicksort.  So, the code below already implements it.
> 
> Regan
> 
> okibi wrote:
> > This does seem like the best way, but I'm not sure how I would go about implement Quicksort into that code. Could you please give me some pointers?
> > 
> > Thanks!
> > 
> > downs Wrote:
> > 
> >> okibi wrote:
> >>> Is there an easy way to sort an array with elements such as "12 - Some text" or "241 - Other Text"?
> >>>
> >>> I want to sort in order of the numbers. Is this possible?
> >>>
> >>> Thanks!
> >> // Sort array via comparison operator.
> >> // 'bigger' returns if 'value' is bigger than 'than'.
> >> void sort(T)(ref T[] array, bool function(T value, T than) bigger) {
> >>    // comparing sorting algorithm goes here. See wikipedia for examples.
> >>    // Quicksort should do nicely and is easy to implement.
> >> }
> >>
> >> long getNumericPart(char[] inp) {
> >>    if (!input.length) return long.max+1; // no number is very small.
> >>    size_t numlen=0;
> >>    while (numlen<inp.length)&&(inp[numlen]>='0')&&(inp[numlen]<='9'))
> >>      ++numlen;
> >>    return inp[0..numlen];
> >> }
> >>
> >> void main() {
> >>    char[][] array=getArray(); // array is read
> >>    array.sort(function(char[] a, char[] b) {
> >>      return getNumericPart(a)>getNumericPart(b);
> >>    });
> >> }
> >>
> >> Didn't try, obviously, but it should work.
> >> Have fun! :D
> >>   --downs
> > 

July 10, 2007
I couldn't get your function to compile. I get the following error:

Error: need 'this' to access member getNumericPart

On the line:

return getNumericPart(a)>getNumericPart(b);

Any ideas?

downs Wrote:

> okibi wrote:
> > Is there an easy way to sort an array with elements such as "12 - Some text" or "241 - Other Text"?
> > 
> > I want to sort in order of the numbers. Is this possible?
> > 
> > Thanks!
> // Sort array via comparison operator.
> // 'bigger' returns if 'value' is bigger than 'than'.
> void sort(T)(ref T[] array, bool function(T value, T than) bigger) {
>    // comparing sorting algorithm goes here. See wikipedia for examples.
>    // Quicksort should do nicely and is easy to implement.
> }
> 
> long getNumericPart(char[] inp) {
>    if (!input.length) return long.max+1; // no number is very small.
>    size_t numlen=0;
>    while (numlen<inp.length)&&(inp[numlen]>='0')&&(inp[numlen]<='9'))
>      ++numlen;
>    return inp[0..numlen];
> }
> 
> void main() {
>    char[][] array=getArray(); // array is read
>    array.sort(function(char[] a, char[] b) {
>      return getNumericPart(a)>getNumericPart(b);
>    });
> }
> 
> Didn't try, obviously, but it should work.
> Have fun! :D
>   --downs

July 10, 2007
okibi wrote:
> This does seem like the best way, but I'm not sure how I would go about implement Quicksort into that code. Could you please give me some pointers?

I often use C's quicksort, found in std.c.stdlib.

void sort(T)(T[] array, bool function(T a, T b) compare) {

    // wrap compare in a C function
    static extern(C) int cmp(void* a, void* b) {
        return compare(*cast(T*)a, *cast(T*)b);
    }

    qsort(array.ptr, array.length, array[0].sizeof, &cmp);
}

compare has to return a negative number if a is smaller, positive is b is smaller, or zero if they are equal.
« First   ‹ Prev
1 2