Example Code

Sort a ListType of structs with ListQuickSort using a user defined comparison function

Code and Documents

Attachment

Overview
The attached program demonstrates how to sort a ListType by using the ListQuickSort function.


Description
The ListType list in the program contains several structs which are created and inserted within the program.  None of the comparison functions provided with the ListQuickSort instrument driver will suffice for sorting the structs within the list so a custom comparison function must be defined in the code.  This custom comparison function is passed references to 2 items from the list, and it is responsible for returning a value based on a comparison which is used to sort the items in the list.  In this example, a list of cars will be sorted based on their year ordered from earliest to latest.  The program inserts the cars into the list, sorts them using the ListQuickSort function, and finally prints out the ordered list in the console.


Requirements

  • LabWindows CVI 2012 (or compatible)


Steps to Implement or Execute Code

  1. Download the attached file to your computer
  2. Open the VI " SortStruct.c"
  3. Run the program

 

 **This document has been updated to meet the current required format for the NI Code Exchange.**

 

Justin D
Applications Engineer
National Instruments
http://www.ni.com/support/

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.