如何通过任意列动态排序数据 [英] How to dynamically sort data by arbitrary column(s)

查看:134
本文介绍了如何通过任意列动态排序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个结构(STL,Boost等)组合的数据结构,这将允许我对内存中的数据排序,就像这里的表格中做的 http://en.wikipedia.org/wiki/List_of_cities_proper_by_population



我想要一个通用的解决方案,可以处理任意数字和列名称。基本上类似
到你使用sql的 order by子句

Is there a data structure of combination of structures ( STL, Boost etc. ) that would allow me to sort data in memory like it is done in this table here http://en.wikipedia.org/wiki/List_of_cities_proper_by_population

I would like a generic solution that can handle arbitray number and names of columns. Essentially similar to what you get using sql's order by clause

推荐答案

允许使用结构或元组来保存aa行:

Consder making a struct or tuple to hold a a row:

struct row{
   unsigned rank;
   std::string city;
   double area;
   //so on
}; 

用您的行填充向量或其他容器。

On populate a vector or other contain with your rows.

std::vector<row> rows;

使用 std :: sort 自定义比较函数,你看一些值。

To sort use std::sort with custom comparison function which you look at certain values.

std::sort(rows.begin(), rows.end(), [](const row& r1, const row& r2)->bool{
    return r1.area < r2.area;
}); //sort by area

这可以通过使用矢量向量和比较函数从它的环境中捕获可变数据:参见我的其他答案

This could be made generic by having a vector of vectors and the comparison function could capture a varaible from it's enviroment: see my other answer

这篇关于如何通过任意列动态排序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆