| 3688 | }; |
| 3689 | |
| 3690 | struct MyItem |
| 3691 | { |
| 3692 | int ID; |
| 3693 | const char* Name; |
| 3694 | int Quantity; |
| 3695 | |
| 3696 | // We have a problem which is affecting _only this demo_ and should not affect your code: |
| 3697 | // As we don't rely on std:: or other third-party library to compile dear imgui, we only have reliable access to qsort(), |
| 3698 | // however qsort doesn't allow passing user data to comparing function. |
| 3699 | // As a workaround, we are storing the sort specs in a static/global for the comparing function to access. |
| 3700 | // In your own use case you would probably pass the sort specs to your sorting/comparing functions directly and not use a global. |
| 3701 | // We could technically call ImGui::TableGetSortSpecs() in CompareWithSortSpecs(), but considering that this function is called |
| 3702 | // very often by the sorting algorithm it would be a little wasteful. |
| 3703 | static const ImGuiTableSortSpecs* s_current_sort_specs; |
| 3704 | |
| 3705 | // Compare function to be used by qsort() |
| 3706 | static int IMGUI_CDECL CompareWithSortSpecs(const void* lhs, const void* rhs) |
| 3707 | { |
| 3708 | const MyItem* a = (const MyItem*)lhs; |
| 3709 | const MyItem* b = (const MyItem*)rhs; |
| 3710 | for (int n = 0; n < s_current_sort_specs->SpecsCount; n++) |
| 3711 | { |
| 3712 | // Here we identify columns using the ColumnUserID value that we ourselves passed to TableSetupColumn() |
| 3713 | // We could also choose to identify columns based on their index (sort_spec->ColumnIndex), which is simpler! |
| 3714 | const ImGuiTableColumnSortSpecs* sort_spec = &s_current_sort_specs->Specs[n]; |
| 3715 | int delta = 0; |
| 3716 | switch (sort_spec->ColumnUserID) |
| 3717 | { |
| 3718 | case MyItemColumnID_ID: delta = (a->ID - b->ID); break; |
| 3719 | case MyItemColumnID_Name: delta = (strcmp(a->Name, b->Name)); break; |
| 3720 | case MyItemColumnID_Quantity: delta = (a->Quantity - b->Quantity); break; |
| 3721 | case MyItemColumnID_Description: delta = (strcmp(a->Name, b->Name)); break; |
| 3722 | default: IM_ASSERT(0); break; |
| 3723 | } |
| 3724 | if (delta > 0) |
| 3725 | return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? +1 : -1; |
| 3726 | if (delta < 0) |
| 3727 | return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? -1 : +1; |
| 3728 | } |
| 3729 | |
| 3730 | // qsort() is instable so always return a way to differenciate items. |
| 3731 | // Your own compare function may want to avoid fallback on implicit sort specs e.g. a Name compare if it wasn't already part of the sort specs. |
| 3732 | return (a->ID - b->ID); |
| 3733 | } |
| 3734 | }; |
| 3735 | const ImGuiTableSortSpecs* MyItem::s_current_sort_specs = NULL; |
| 3736 | } |
| 3737 |
no outgoing calls
no test coverage detected