3

I have an array called g_Parameters that I am looking to sort alphabetically in a new array called g_SortedParameters.

I have provided the code for the array. I need to sort it alphabetically to use in a log, but without g_Parameters itself being sorted alphabetically as other code relies on the indexing as it stands.

const unsigned long ParamsCount = 6;
const TCHAR * const g_Parameters[ParamsCount] =
{ TEXT("AllowFatal"), TEXT("Variant"), TEXT("EnableLogs"),
  TEXT("AllowRescan"), TEXT("Path"), TEXT("Filter"),
};

I would appreciate any help with sorting this array into a new array. I'm happy to provide more information if it is required.

7
  • 1
    how about std::sort ? Commented Sep 4, 2019 at 12:46
  • 1
    The cleanest would be to copy the content of array with std::copy and sort the copy with std::sort, but I'm not sure how well is that working with winapi. Commented Sep 4, 2019 at 12:48
  • 4
    std::partial_sort_copy() can be used to copy and sort at the same time without modifying the original. Commented Sep 4, 2019 at 12:52
  • 3
    ParamsCount = 3? I would count 6 elements in array. but missing {. Commented Sep 4, 2019 at 12:52
  • 1
    Whenever you have a problem like this, your first step should be to browse through the list of algorithms provided by the standard library. Commented Sep 4, 2019 at 13:07

1 Answer 1

3

One way is to use std::partial_sort_copy() which, despite the name, can be used to sort a copy of an entire array without modifying the order of the original.

Demonstration:

#include <algorithm>
#include <array>
#include <iostream>

template<class T, std::size_t N>
void print_array(const std::array<T, N> &ar) {
  for (const auto &elem : ar) {
    std::cout << elem << ' ';
  }
  std::cout << '\n';
}

int main() {
  std::array<int, 5> orig = { 10, 8, 1, 4, 3 }, sorted;
  std::partial_sort_copy(orig.begin(), orig.end(), sorted.begin(), sorted.end());
  print_array(orig);
  print_array(sorted);
}

when compiled and run will print out

10 8 1 4 3 
1 3 4 8 10 
Sign up to request clarification or add additional context in comments.

4 Comments

However, you'll also need to teach predicates since the array contains C-strings.
@LightnessRacesinOrbit Or just tell OP to use std::string instead of whatever TCHAR * and TEXT() are. This is just to give him an idea of how to use the function. Adding a custom comparison function is left as an exercise for the reader.
Perhaps as a bonus, but as it currently stands this answer is insufficient for the question posed. When they apply this solution, they will get the wrong answer. You could leave the predicate as an exercise to the reader, but you should at least mention that the exercise exists.
@Shawn - "Or just tell OP to use std::string instead of whatever TCHAR * and TEXT() are." No. These can't be changed like that and must remain as they are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.