### TAGSORT [DESCENDING] inputVector resultVariable

 Replaces the contents of the result vector with a list whose element values, in order, are the positions of the elements of the input vector were they sorted in ascending order. In other words the first element of the result variable tells which element of the input would be first, the second element of the result tells which element of the input would be second, etc. The order just described will be reversed if you "TAGSORT descending". This command can be used with TAKE to coordinate the elements of two or more vectors whose elements are related. One vector contains the "keys" on which a sort is based and the other has data that needs to follow the keys into the proper order. You can think of each vector as being a column in a table. Then, the tagsort is used to sort the table's rows according to the values in one column. See the first example to the right. Missing data, represented by "NaN" or ".", is assumed to be larger than the largest value than can be expressed. Therefore NaNs will rank at the end in the ascending case and at the beginning in the descending case. If you want to remove NaNs from your vectors, use the CLEAN command. TAGSORT will perform its sort into the same order that the SORT command would use. See the third example at right. For a full explanation of the sorting of mixed vectors (vectors containing numbers and Named Constants), see the SORT command. Usage with the LET command You can use TAGSORT as a function in the LET command. By default, it will do an ascending sort. If you want to use it to do a descending sort, use the name TAGSORT_DESCENDING, which is only valid within a LET command. Here is a program illustrating the use of the TAGSORT command to sort multiple vectors into an order determined by one of the vectors. ```'Given a data set consisting of a list of students, 'their weights (in pounds) and heights (in inches), 'sort all three vectors in the order of their weights. COPY (123 156 192 131 205 174) studentNumber COPY (125 132 210 101 187 165) studentWeight ' in pounds COPY (60 65 72 63 68 68) studentHeight ' in inches OUTPUT "\nBefore Sorting:\nWeight Height Student #\n" WRITE WINDOW studentWeight%6.0f studentHeight%6.0f studentNumber%6.0f TAGSORT studentWeight weightTags TAKE studentNumber weightTags numberSorted TAKE studentWeight weightTags weightSorted TAKE studentHeight weightTags heightSorted OUTPUT "\nAfter Sorting:\nWeight Height Student #\n" WRITE WINDOW weightSorted%6.0f heightSorted%6.0f numberSorted%6.0f ``` The above program produces the following output: ```Before Sorting: Weight Height Student # 125 60 123 132 65 156 210 72 192 101 63 131 187 68 205 165 68 174 After Sorting: Weight Height Student # 101 63 131 125 60 123 132 65 156 165 68 174 187 68 205 210 72 192 ``` This next program shows the effect of NaN on the tagsort: ```COPY (3 5 NaN 2 4 1) A TAGSORT A B TAGSORT descending A C PRINT A B C TAKE A B AB TAKE A C AC PRINT AB AC``` The above program produces the following output: ```A: (3.0 5.0 NaN 2.0 4.0 1.0) B: (6.0 4.0 1.0 5.0 2.0 3.0) C: (3.0 2.0 5.0 1.0 4.0 6.0) AB: (1.0 2.0 3.0 4.0 5.0 NaN) AC: (NaN 5.0 4.0 3.0 2.0 1.0)``` Here's an example showing the tagsort on a mixed vector: ```NAME Jan Feb Mar 'Doesn't give values to these names NAME 1,3 Ace Deuce Trey 'Gives values to these names 'Make a mixed vector: COPY 1,3 jan,mar Ace,Trey vec 'Numbers and both kinds of names 'TagSort vec to show how the different 'types interact: SORT vec vecSorted TAGSORT vec Tags PRINT table vec Tags vecSorted``` And here is the output: ```vec Tags vecSorted 1 4 Jan 2 5 Feb 3 6 Mar Jan 1 1 Feb 7 Ace Mar 2 2 Ace 8 Deuce Deuce 3 3 Trey 9 Trey ```