Sorting an array using multiple sort criteria (QuickSort)

前端 未结 5 1655
傲寒
傲寒 2020-12-21 05:09

I am trying to find out how (using a quicksort algorithm) to sort an struct array by 2 criterias. For example say I had a struct of:

struct employee{
   char         


        
5条回答
  •  半阙折子戏
    2020-12-21 05:48

    It's not hard. You just need a function (or block of code seeing you want it "hard coded") to compare your structs. In the sample code you've given you are comparing the current object using arr[l].id <= arr[p].id. That is you are only considering id to work out where your element fits. You just need to compare using the other fields at this point. It would be much tidier with a function (I gave you such a function in your earlier question).

    You are also only shifting the id fields when you swap - leaving the name and gender unchanged in your data items. You should move the whole struct.

提交回复
热议问题