TutPtrArySort


Name

TutPtrArySort — sort a pointer array

Synopsis

T_BOOL TutPtrArySort(ptr_ary, compare_func) 
T_PTR_ARY ptr_ary; 
T_PTR_ARY_CMP_FUNC compare_func; 

Arguments

ptr_ary — pointer array to sort.

compare_func — user-defined function used to compare the elements in the array while sorting. This argument is mandatory and should never be NULL.

Return Values

TRUE if successful, FALSE otherwise.

Diagnostics

None

Description

TutPtrArySort sorts the elements in ptr_ary. The array is sorted using the user-supplied compare_func. That function must always be supplied. The comparison function should have return values equivalent to that of the strcmp function — it should return 0 when elements are equal, -1 when the first element is less than the second element, and 1 when the first element is greater than the second element. Note that the sort changes the order in which elements are stored in the array.

TutPtrArySort calls the pointer array change callbacks with the reason T_PA_SORTED.

Caution

None

See Also

TutPtrAryCreate

Examples

This example sorts a pointer array:

T_INT4 compare_func(item1, item2) 
T_PTR item1; 
T_PTR item2; 
{ 
  if (*item1 == *item2) { 
    return 0; 
  }  
  if (*item1 < *item2) { 
    return -1; 
  } 
 
  if (*item1 > *item2) { 
    return 1; 
  } 
} 
T_PTR_ARY ptr_ary; 
T_INT4 i, *int_ptr; 
 
/* create the pointer array with initial size of 30 */ 
 
ptr_ary = TutPtrAryCreate(30); 
/* seed the random number generator */ 
srand(255); 
/* fill it up with 25 entries */ 
for (i = 0; i < 25; i++) { 
  /* allocate the space for the next element */ 
  T_MALLOC(int_ptr, sizeof(T_INT4), T_INT4 *); 
  *int_ptr = rand(); 
  /* append the item to the array, ignore the index */ 
  TutPtrAryAppend(ptr_ary, int_ptr, NULL); 
}  
 
/* sort the array of random integers */ 
TutPtrArySort(ptr_ary, compare_func); 

TIBCO SmartSockets™ Utilities
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com