LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Sort linked list by record field

Hello!  How do I use ListQuickSort and its compare function to sort a linked list according to a given data field within each list item?  In other words, if I have an integer "depth" field in my list records, how do I force the compare function to sort the list by increasing/decreasing depth?
 
Thanks!
 
Slowpoke 
Slowpoke
CLAD (believe it or not!), using LabVIEW 8.5

They don't call me "Slowpoke" for nothin'!
0 Kudos
Message 1 of 3
(3,296 Views)
You supply/designate the compare function for the sort, just write one to do that comparison. Are your list items structs?

CompareFunction (void *item1, void *item2) {

  if (item1->integerfield > item2->integerfield) return 1;
  else if (item1->integerfield < item2->integerfield) return -1;
  return 0;

}

Menchar



0 Kudos
Message 2 of 3
(3,293 Views)
Yes, my list items are structs called "NODE", which contain a field called "depth".  Your post was just the hint I needed.  Here's the final form of the compare function:

int CVICALLBACK SortByDepth (void *item1, void *item2)
{
    NODE *itemA, *itemB;
    double a, b;
   
    itemA = (NODE*)item1;
    itemB = (NODE*)item2;
   
    a = itemA->depth;
    b = itemB->depth;
   
    if (a > b)
        return 1;
    if (a == b)
        return 0;
    else
        return -1;
}

Works like a charm.  Thanks again!


Slowpoke
Slowpoke
CLAD (believe it or not!), using LabVIEW 8.5

They don't call me "Slowpoke" for nothin'!
0 Kudos
Message 3 of 3
(3,260 Views)