Reputation: 217
I have a C structure that contains contact info for a person, such as name, phone number, etc. The "contact" structures are contained within a linked list. I need to insert nodes in a way such that the linked list is sorted into alphabetical (ascending) order.
Is there a built-in sorting function in C that I can call? Or do I have to write my own sorting function? If there is a built-in function, could I get an example of how I'd call it on a structure within a linked list?
Upvotes: 2
Views: 2114
Reputation: 10370
Here is an example of code that performs an insertion sorted linked list. This isn't a cut and paste for you, but will show you what to expect in that type of insertion:
http://www.c.happycodings.com/Sorting_Searching/code8.html
Focus on the "insert()" call. I did not compile this code to test it, but I read it over and it looks correct to me. It shows how to search a list and pointer adjustments. You should be able to solve your problem from this code.
Upvotes: 0
Reputation: 182664
There's no standard sorting method for a "list". The closest is qsort
(which can indeed sort user-defined objects) but it only works on continuous ranges (arrays and the like).
You'll probably have to implement your own sorting procedure or use and array instead of a list.
Upvotes: 2