tealeg / xlsx

Go library for reading and writing XLSX files.
Other
5.88k stars 819 forks source link

Sorting #829

Open mbinette opened 1 week ago

mbinette commented 1 week ago

Is anyone sorting with v3? I was on v1 and Sheet.Rows was a slice, and we implemented the sort interface (Less and Swap). I can use the new Sheet.Row(index) function to get a pointer to the row, but I can't see how to implement Swap since I don't have access to the underlying cellStore. In v1 we could do Sheet.Row[a], Sheet.Row[b] = Sheet.Row[b], Sheet.Row[a] to swap to rows in the sheet.

We are actually sorting by multiple columns. I don't see any way of adding anything to tell Excel how to sort. I did see a closed issue requesting that feature.

If I knew how to swap two rows, my current implementation would continue to work.

tealeg commented 1 week ago

@mbinette I'm kind of hesitant to expose the cellstore implementation at runtime. It might make sense to add an implementation of the sort.Interface to the CellStore and CellStoreRow interfaces.

mbinette commented 1 week ago

I think if you were to add anything to the CellStore interface, it would be something like the OrderedBy example at https://pkg.go.dev/sort as the Example (SortMultiKeys).

Looking at the default MemoryCellStore and MemoryRow, I think I can probably copy it and modify a little to create a SortedCellStore and SortedMemoryRow. I know the sort order before I add data so I could sort as data is added or add the sort.Interface to just my own custom store.

tealeg commented 6 days ago

@mbinette - yes, the MemoryCellStore is probably quite easy to make sortable. The DiskVCellStore would be harder