1

I'm trying to sort a column by a custom list that is in no specific order.

Example:

This is how data starts:

enter image description here

This is how data SHOULD be after sort:

enter image description here

This is how data currently gets sorted by below code:

enter image description here

Code:

var ws = context.workbook.worksheets.getActiveWorksheet();
ws.getRange("A1").values = "Col_Header"
ws.getRange("A2").values = "D"
ws.getRange("A3").values = "Z"
ws.getRange("A4").values = "C"
ws.getRange("A5").values = "B"
await context.sync()
var Used_Rng_And_Props = ws.getUsedRange(true)
Used_Rng_And_Props.load(['rowCount', 'columnCount'])
await context.sync()
var sort_rng = ws.getRangeByIndexes(1, 0, Used_Rng_And_Props.rowCount - 1, Used_Rng_And_Props.columnCount);
sort_rng.select()
var sort_arr = ['C', 'B', 'Z']
var col_index = 0
// Sort the range
const sortFields = [
    {
        key: col_index,
        //ascending: false, //true = Sort alphanumeric which defeats purpose
        sortOn: Excel.SortOn.value, //value or values seem to work, not sure which is correct, both procude same result.
        values: sort_arr,
    }
];
sort_rng.sort.apply(sortFields);
await context.sync()

Docs: https://learn.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.sortfield?view=office-scripts

Update:

This is how you would do it in VBA:

Sub runSortC()
    Dim vCustom_Sort As Variant, rr As Long

    vCustom_Sort = Array("C", "B", "Z")
    Application.AddCustomList ListArray:=vCustom_Sort

    With Range("A:A")
        .Parent.Sort.SortFields.Clear

        'sort on custom order with header
        .Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _
                    Orientation:=xlTopToBottom, Header:=xlYes, MatchCase:=False, _
                    OrderCustom:=Application.CustomListCount + 1

        .Parent.Sort.SortFields.Clear

    End With

End Sub

This is how you would do it via Excel UI:

https://support.microsoft.com/en-us/office/sort-data-using-a-custom-list-cba3d67a-c5cb-406f-9b14-a02205834d72

FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57
  • What is the sort logic for the desired result? – David Leal May 09 '23 at 02:50
  • Its supposed to sort based on the sort_arr @DavidLeal – FreeSoftwareServers May 09 '23 at 04:52
  • I'm starting to think it isn't possible w/ the JS API? Its easy w/ VBA, so this would be an example of how the JS API isn't mature enough yet, but maybe I'm wrong... – FreeSoftwareServers May 09 '23 at 19:28
  • I was able to use JS with Excel. [Here](https://stackoverflow.com/questions/76175455/how-do-i-get-a-list-of-permutations-in-one-cell/76195803#76195803) is an example of that using a custom function. What you have is a script. For me, your example is very simple only one column, so I don't fully understand what you want to achieve. Per my understanding, you need to get the range data, sort it, and write it back. – David Leal May 09 '23 at 20:43
  • If you have one column and sorting criteria based on the element of the given list, the sorting criteria is equal to the output. I would suggest trying to provide more details about the problem to solve here. – David Leal May 09 '23 at 20:49
  • @DavidLeal I don't understand what is complicated. You have a column in the order shown under "DATA START" and I want to sort it, via JavaScript, based on a custom list, to be in the order of "DESIRED RESULT". But, as you've requested further details, I've added how to accomplish the task via VBA and Excel UI. Hopefully that can help. There should be no "Writing the data back", that is what I'm trying to avoid, otherwise I'd get the data as an Arr_Of_Objs and sort it via JS and write it back, but I'd risk losing worksheet properties and I shouldn't have to re-write the data. – FreeSoftwareServers May 09 '23 at 21:03
  • @DavidLeal I have no idea how that link is related to this post. Are you familiar with Office-JS framework? I think perhaps there might be some misunderstanding that is getting in the way here. – FreeSoftwareServers May 09 '23 at 21:11
  • Maybe I am not following you, if you have an array: `{"A";"B";"C"}` and you want to sort it as follows: `{"C";"B";"A"}` the sorting criteria is in fact your output, so I don't understand what logic needs to be built. If you have more than one column, then it makes sense, but for one column I don't see the point. – David Leal May 09 '23 at 21:15
  • @DavidLeal I'm not trying to sort an `array`, I'm trying to sort a `range`. It shouldn't matter who many columns I have or how large the range is, did you check out the link for how to do it via the Excel UI? That might make it clearer? – FreeSoftwareServers May 09 '23 at 21:17
  • I see, for the function you are trying to use there is no too much documentation on how works `Excel.RangeSort.apply` method. If you really want to use it, I would suggest to post an issue [here](https://github.com/OfficeDev/office-js-docs-reference/issues) since it is not documented on how to use it with no specific sample. Other option I would to try to use javascript functions and then update the range. – David Leal May 10 '23 at 13:05
  • 1
    @DavidLeal Yeah, I'm waiting till bounty ends to see if anyone can answer re using official `rng.sort.apply(sortFields);` and then I'll post an answer which will re-write the range and sort via `JS`. This should be possible via JS API though, as you can see, its possible via VBA. – FreeSoftwareServers May 10 '23 at 16:15
  • I don't think the function is intended to sort by another array. The field: `values: sort_arr` in `sortFields` array has no effect, since it is not such property in `Excel.SortField` interface. When the property `ascending` is not provided, it assumes `null` so there is no sorting criteria. That is why you get this result. My conclusion would be sorting by another array is not an option for: `sort.apply` has. – David Leal May 11 '23 at 03:11
  • 1
    So...., Your saying my code doesn't work ;), yes thats the question isn't it. – FreeSoftwareServers May 11 '23 at 06:55

0 Answers0