COMMUNITY

How to implement custom sort function?

public static function sort(target:Bytes, f:(a:Bytes,b:Bytes)->Int):Void {

	  //how to do ?


}

I want to do some thing like

		  var arr=[[1,2,3],[4,5,6]];

	  arr.sort(function (a,b){return a[0]>b[0]?-1:1 });

but I want to Bytes instead of Array<Array>;

    var bytes=Bytes.alloc(6);
      bytes.set(0,1);
      bytes.set(1,2);
      bytes.set(2,3);
      bytes.set(3,4);
      bytes.set(4,5);
     bytes.set(5,6);
     sort(bytes,function(a,b){......//how to write here?

It’s not clear what you are asking because
var arr = [[1,2,3],[4,5,6]];
is an array having two elements, each of which is an array having three elements, each of those is an Int. And your sort example sorts the two-element array, by the value of their first elements.

It is not the same as
var bytes = Bytes(6);
which is an array having six elements, each of which is a Byte.

The typical sort for a Byte array would be sort(bytes, (a,b)->a-b);. If you need to normalize the result values to (-1,0,1), then sort(bytes, (a,b)->a == b ? 0 : a > b ? 1 : -1);

Is that what you are asking?

If you want to treat the elements of the Byte array as discrete sets of three elements, then you will have to implement a custom sort routine, because a normal sort on an array will sort all elements, NOT sets of elements.

thank you for your answer

and here is what I done here

and what I want is

multiple thread + quick sort on bytes

but I still not finish now .

You need to study merge sort to see how to solve your problem. And we’re beyond Haxe issues here… this looks like homework. :slight_smile: