使用C++对列中的数据进行排序并使用自定义排序列表

可能的使用场景

你可以使用自定义列表对列中的数据进行排序。这可以通过 DataSorter::AddKey(int key, SortOrder order, String customList) 方法完成。然而,此方法仅在自定义列表中的项目没有逗号时有效。如果项目中有逗号,比如 “USA,US”、“中国,CN” 等,则必须使用 **DataSorter::AddKey Method (Int32, SortOrder, String[])** 方法。这里,最后一个参数不是字符串,而是字符串数组。

使用自定义排序列表对列中的数据进行排序

以下示例代码说明了如何使用 **DataSorter::AddKey Method (Int32, SortOrder, String[])** 方法通过自定义排序列表对数据进行排序。请查看此代码所用的 示例Excel文件 和由其生成的 输出Excel文件。下图显示了代码执行后对样本Excel文件的效果。

todo:image_alt_text

示例代码

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Load the source Excel file
    Workbook wb(srcDir + u"sampleSortData_CustomSortList.xlsx");

    // Access first worksheet
    Worksheet ws = wb.GetWorksheets().Get(0);

    // Specify cell area - sort from A1 to A40
    CellArea ca = CellArea::CreateCellArea(u"A1", u"A40");

    // Create Custom Sort list
    Vector<U16String> customSortList = { u"USA,US", u"Brazil,BR", u"China,CN", u"Russia,RU", u"Canada,CA" };

    // Add Key for Column A, Sort it in Ascending Order with Custom Sort List
    wb.GetDataSorter().AddKey(0, SortOrder::Ascending, customSortList);
    wb.GetDataSorter().Sort(ws.GetCells(), ca);

    // Save the output Excel file
    wb.Save(outDir + u"outputSortData_CustomSortList.xlsx");

    std::cout << "Data sorted successfully with custom sort list!" << std::endl;

    Aspose::Cells::Cleanup();
}