Syntax
ITEM *ASort( ITEM *array, int start, int count, ASortBlock order, void *cargo );
Arguments
array Array to sort.
start First position, counted from 1. NIL or a value below 1 starts at 1.
count Number of elements to sort. NIL sorts through the end of the array.
order NULL for ascending order, or a function called with two elements: xBool order( const ITEM *first, const ITEM *second, void *cargo ). It returns T when first comes before second.
cargo Any pointer, passed unchanged to each call of order. It takes the place of variables outside the block; NULL when not needed.
Return value
The array, or NULL if array is not an array.
Description
Sort the requested range in place. The default order is ascending. It compares strings by bytes, placing a shorter prefix before a longer string that starts with it. With `Set( _SET_EXACT, T )`, trailing spaces do not count. Dates sort chronologically, numbers by value, and `F` precedes `T`.
The default mixed-type order groups arrays, blocks, strings, logical values, dates and numbers, then the remaining types. Supply an order callback to compare subarrays by their contents or to define a different ordering. The callback receives item copies, with references to any original subarrays.
Equal elements can change their relative order. If a callback shortens the array and the requested sort range no longer fits, the sort stops.
Example
#include "xdev-wtui.h"
#include <stdio.h>
static xBool descending( const ITEM *first, const ITEM *second, void *cargo )
{
( void )cargo;
return ItemGetNI( first ) > ItemGetNI( second ) ? T : F;
}
/* The subarrays { name, age } in the order of the age */
static xBool byAge( const ITEM *first, const ITEM *second, void *cargo )
{
( void )cargo;
return ArrayGetNI( first, 2 ) < ArrayGetNI( second, 2 ) ? T : F;
}
static void printNumbers( const char *title, const ITEM *array )
{
printf( "%s:", title );
for( size_t i = 1; i <= ( size_t )ArrayLen( array ); ++i )
printf( " %d", ArrayGetNI( array, i ) );
printf( "\n" );
}
int main( void )
{
static const char *names[] = { "Mary", "Joe", "Art" };
static const int ages[] = { 14, 23, 16 };
static const int values[] = { 3, 5, 1, 2, 4 };
ITEM *numbers = Array( 5 );
ITEM *kids = Array( 3, 2 );
if( !numbers || !kids )
{
ItemRelease( numbers );
ItemRelease( kids );
return 1;
}
for( size_t i = 1; i <= 5; ++i )
ArraySetNI( numbers, i, values[ i - 1 ] );
ASort( numbers, NIL, NIL, NULL, NULL );
printNumbers( "Ascending", numbers );
ASort( numbers, NIL, NIL, descending, NULL );
printNumbers( "Descending", numbers );
for( size_t i = 1; i <= 3; ++i )
{
ArraySetC( ArrayGetItemPtr( kids, i ), 1, names[ i - 1 ] );
ArraySetNI( ArrayGetItemPtr( kids, i ), 2, ages[ i - 1 ] );
}
ASort( kids, NIL, NIL, byAge, NULL );
for( size_t i = 1; i <= 3; ++i )
printf( "%s %d\n", ArrayGetCPtr( ArrayGetItemPtr( kids, i ), 1 ), ArrayGetNI( ArrayGetItemPtr( kids, i ), 2 ) );
ItemRelease( numbers );
ItemRelease( kids );
return 0;
}
Output
Ascending: 1 2 3 4 5
Descending: 5 4 3 2 1
Mary 14
Art 16
Joe 23