digitalmars.D.learn - Custom compare function for array.sort on an integer array?
- Sequ (10/10) Apr 19 2011 Like the topic says, is it possible to set a custom compare function, fo...
- Dmitry Olshansky (9/19) Apr 19 2011 If you are talking use std.algorithm sort
- Dmitry Olshansky (5/6) Apr 19 2011 Should be: "If your are talking about D2, then use std.algorithm sort
- Sequ (1/9) Apr 19 2011 Ah, yes, thanks; that looks like it could be perfect.
Like the topic says, is it possible to set a custom compare function, for when you are using the 'sort' property of an integer array? I want the integers to be sorted by a different criteria than their natural order. From the documentation (http://d-programming-language.org/arrays.html) I can see how it would be done for structs or objects, but it doesn't seem to be possible for primitive types. If it can't yet be done, then I'm sure that adding the ability to give a comparison function or lazy expression to the 'sort' call would be very useful. Unless there is some reason that that would be beyond the scope of the 'sort' property's purpose?
Apr 19 2011
On 19.04.2011 16:57, Sequ wrote:Like the topic says, is it possible to set a custom compare function, for when you are using the 'sort' property of an integer array? I want the integers to be sorted by a different criteria than their natural order. From the documentation (http://d-programming-language.org/arrays.html) I can see how it would be done for structs or objects, but it doesn't seem to be possible for primitive types. If it can't yet be done, then I'm sure that adding the ability to give a comparison function or lazy expression to the 'sort' call would be very useful. Unless there is some reason that that would be beyond the scope of the 'sort' property's purpose?If you are talking use std.algorithm sort Like taken from docs below: bool myComp(int x,int y) {return x> y; } sort!(myComp)(array); See also: http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#sort -- Dmitry Olshansky
Apr 19 2011
On 19.04.2011 16:56, Dmitry Olshansky wrote:If you are talking use std.algorithm sortShould be: "If your are talking about D2, then use std.algorithm sort ", ouch :) -- Dmitry Olshansky
Apr 19 2011
If your are talking about D2, then use std.algorithm sort Like taken from docs below: bool myComp(int x,int y) {return x> y; } sort!(myComp)(array); See also: http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#sort -- Dmitry OlshanskyAh, yes, thanks; that looks like it could be perfect.
Apr 19 2011