V
V
VerNika2016-05-02 13:13:23
Programming
VerNika, 2016-05-02 13:13:23

Internal sort. What is the best way to sort by multiple fields?

In short, let's say we have N records of the form:

struct Node {
   string a, b;
   int c, d, e;
};

The user enters 3 numbers - the numbers of the fields by which our N records need to be sorted.
Sorting - internal, using a binary tree. With sorting by one field, everything is clear and understandable - we build a tree using the selected field as keys.
What about sorting by multiple fields?
UPD # 1
Of course, as an option, for each field, build your own binary tree and sort by the first field-tree, and suddenly, if the keys match, sort by the next field-tree, etc. Will that be right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandra Vorontsova, 2016-05-02
@VerNika

Why not make a simple procedure for comparing objects of type Node? usually it is implemented by the cmp function or built-in "magic" methods, as in python.
And then feed this comparing function into the sort function as one of the parameters.
Similarly, you can use the result of this function when building a binary tree in order to decide which subtree the current vertex should belong to.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question