Algorithm for sorting within windows
What you require is the lexicographic ordering. Let me describe this.
Suppose the speeches have two attributes. The first one is &quot;speaker's name&quot; and the second one is the &quot;length&quot;. For simplification, let me define the speaker's name by the integer value instead of a character string. Now, suppose the speeches are represented using the following tuples: $(0,0)$, $(1,2)$, $(0,3)$, $(1,4)$, $(2,3)$, $(2,4)$, and $(1,1)$. Here, the second speech has the speaker's name: $1$, and length $= 2$.
Requirement 1: You simply want to sort the speeches by the &quot;speaker's name&quot;.
Just consider the first attribute for comparing any two speeches and apply any sorting algorithm. You will get the following ordering: $(0,0)$, $(0,3)$, $(1,2)$, $(1,4)$, $(1,1)$, $(2,3)$, and $(2,4)$.
Requirement 2: You want to sort first by the &quot;speaker's name&quot; and then by the &quot;length&quot;. This is the requirement that you have mentioned in your question.
Now, define a new comparison operation between two tuples as follows: $(a,b) &lt; (c,d)$ if and only if $(a&lt;c)$ or $(a = c$ and $b&lt;d)$
Here, we are comparing by giving the &quot;first attribute&quot; more priority. And if the first attribute value is the same, we compare based on the &quot;second attribute&quot;.
Smilarly, you can define $(a,b) &gt; (c,d)$ if and only if ($a&gt;c$) or ($a = c$ and $b&gt;d$).
Now, you can simply use any sorting algorithm by replacing the standard comparison operation with the above-defined comparison operation.
You will get the following ordering: $(0,0)$, $(0,3)$, $(1,1)$, $(1,2)$, $(1,4)$,  $(2,3)$, and $(2,4)$.
Requirement 3: Suppose, you want to sort by the length and then by the speaker's name
Simply, define the comparison operation between two tuples as: $(a,b) &lt; (c,d)$ if and only if $(b&lt;d)$ or $(b = d$ and $a&lt;c)$. Now, again you can use any standard sorting algorithm by replacing the standard comparison operation with this comparison operation.
You will get the following ordering: $(0,0)$, $(1,1)$, $(1,2)$, $(0,3)$, $(2,3)$, $(1,4)$,  and $(2,4)$.

Similarly, you can define a new comparison operation for the multiple attributes based on your requirements.