文件名按照自定义类型大小排序

1 篇文章 0 订阅

按照字符串对应的数字数值大小排序 


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int main() {
    std::vector<std::string> a = {"11", "12", "13", "21", "22", "111", "109", "136", 
        "255"};
    std::sort(a.begin(), a.end(), [](std::string str1, std::string str2)-> bool {
                int n1 = std::stoi(str1);
                int n2 = std::stoi(str2);
                return n1 < n2;
            });
    for (auto it = a.begin(); it != a.end(); it++) {
        std::cout << *it << std::endl;
    }
    return 0;
}

按照多个规则排序

假设有一组文件命名规则如下:

112_1_229775155990.jpg

// index , type下有0,1,2,3 四种类型, 后面为时间戳。

需要按照时间戳优先,然后是 type类型进行排序


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

uint64_t getTimeByFilename(std::string filename) {
    auto begin = filename.find_last_of("_");
    auto end = filename.find_last_of(".");
    std::string timestr = filename.substr(begin + 1, end - begin - 1);
    uint64_t t = std::stoull(timestr);
    return t;
}

int getImageType(std::string filename) {
    auto end = filename.find_last_of("_");
    auto begin = filename.find_first_of("_");
    std::string typestr = filename.substr(begin + 1, end - begin -1);
    return std::stoi(typestr);
}

int main() {

    std::vector<std::string> a = {
"112_1_229775155990.jpg", "136_2_241161599982.jpg", "15_3_208948787543.jpg", "184_0_264204662581.jpg", "207_1_277262210610.jpg", "230_2_288372441910.jpg", "254_3_301398403208.jpg", "44_1_211804343887.jpg"  ,"68_2_214299957193.jpg",  "91_3_220470985535.jpg",
"112_2_229775155990.jpg", "136_3_241161599982.jpg", "160_0_251871466897.jpg", "184_1_264204662581.jpg", "207_2_277262210610.jpg", "230_3_288372441910.jpg", "255_0_301879357977.jpg", "44_2_211804343887.jpg" , "68_3_214299957193.jpg",  "92_0_220898811766.jpg",
"112_3_229775155990.jpg", "137_0_241589578905.jpg", "160_1_251871466897.jpg", "184_2_264204662581.jpg", "207_3_277262210610.jpg", "231_0_288905456640.jpg", "255_1_301879357977.jpg", "44_3_211804343887.jpg" , "69_0_214408638886.jpg",  "92_1_220898811766.jpg",
"113_0_230838035835.jpg", "137_1_241589578905.jpg", "160_2_251871466897.jpg", "184_3_264204662581.jpg", "208_0_277585340763.jpg", "231_1_288905456640.jpg", "255_2_301879357977.jpg", "45_0_211913428041.jpg" , "69_1_214408638886.jpg",  "92_2_220898811766.jpg",
"113_1_230838035835.jpg", "137_2_241589578905.jpg", "160_3_251871466897.jpg", "185_0_264691801119.jpg", "208_1_277585340763.jpg", "231_2_288905456640.jpg", "255_3_301879357977.jpg", "45_1_211913428041.jpg" , "69_2_214408638886.jpg",  "92_3_220898811766.jpg",
"113_2_230838035835.jpg", "137_3_241589578905.jpg", "161_0_252932160435.jpg", "185_1_264691801119.jpg", "208_2_277585340763.jpg", "231_3_288905456640.jpg", "256_0_302305657784.jpg", "45_2_211913428041.jpg" , "69_3_214408638886.jpg",  "93_0_221323990534.jpg",
"113_3_230838035835.jpg", "138_0_242016079443.jpg", "161_1_252932160435.jpg", "185_2_264691801119.jpg", "208_3_277585340763.jpg", "232_0_289748549409.jpg", "256_1_302305657784.jpg", "45_3_211913428041.jpg" , "6_0_208095979313.jpg",  "93_1_221323990534.jpg",
"114_0_231633976296.jpg", "138_1_242016079443.jpg", "161_2_252932160435.jpg", "185_3_264691801119.jpg", "209_0_278117674802.jpg", "232_1_289748549409.jpg", "256_2_302305657784.jpg", "46_0_212030822080.jpg" , "6_1_208095979313.jpg",  "93_2_221323990534.jpg",
"114_1_231633976296.jpg", "138_2_242016079443.jpg", "161_3_252932160435.jpg", "186_0_265227930195.jpg", "209_1_278117674802.jpg", "232_2_289748549409.jpg", "256_3_302305657784.jpg", "46_1_212030822080.jpg" , "6_2_208095979313.jpg",  "93_3_221323990534.jpg",
"114_2_231633976296.jpg", "138_3_242016079443.jpg", "162_0_253520996588.jpg", "186_1_265227930195.jpg", "209_2_278117674802.jpg", "232_3_289748549409.jpg", "257_0_302743606361.jpg", "46_2_212030822080.jpg" , "6_3_208095979313.jpg",  "94_0_221757048342.jpg",
"114_3_231633976296.jpg", "139_0_242451148981.jpg", "162_1_253520996588.jpg", "186_2_265227930195.jpg", "209_3_278117674802.jpg", "233_0_290853577408.jpg", "257_1_302743606361.jpg", "46_3_212030822080.jpg" , "70_0_214467849386.jpg",  "94_1_221757048342.jpg",
"115_0_232060007565.jpg", "139_1_242451148981.jpg", "162_2_253520996588.jpg", "186_3_265227930195.jpg", "20_0_209448189505.jpg", "233_1_290853577408.jpg", "257_2_302743606361.jpg", "47_0_212091476964.jpg"  ,"70_1_214467849386.jpg",  "94_2_221757048342.jpg",
    };
    std::sort(a.begin(), a.end(), [](std::string str1, std::string str2)-> bool {
                uint64_t n1 = getTimeByFilename(str1);
                uint64_t n2 = getTimeByFilename(str2);
                int t1 = getImageType(str1);
                int t2 = getImageType(str2);
                if (n1 != n2) {
                    return n1 < n2;
                } else {
                    return t1 < t2;
                }
            });
    for (auto it = a.begin(); it != a.end(); it++) {
        std::cout << *it << std::endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值