无论对象变量类型如何,如何对对象数组进行排序 [英] How Sort an array of objects regardless of object variable type

查看:85
本文介绍了无论对象变量类型如何,如何对对象数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Record对象数组,每个Record对象都有5个字段(名字,姓氏,GPA,ID号和电子邮件)。我想根据归因于该对象的任何变量对数组进行排序。我的教授说,有一种方法可以使用一个函数对其进行排序,而不管传递的变量类型如何。但是我想不出一种方法可以对这5个变量和3种不同的变量类型中的任何一个进行排序。这意味着我不能仅将每个变量的粘贴函数复制粘贴5次一次。到目前为止,我可以按单个值对数组进行排序,例如record []。GPA,但我需要一种类似于record []。x的方式,其中x是用户决定我们对数组进行排序的变量。

I have an array of Record objects and each record object has 5 fields (first name, last name, GPA, ID number,and email). I want to sort the array based on any of the variables attributed to the object. My professor says there is a way to use one function that will sort it regardless of the variable type that is passed through. But I can't figure out a way to have one function that can sort any of those 5 variables and 3 different variable types. Meaning I can't just copy paste my sort function 5 times once for each variable. So far I can sort the array by a single value, like record[].GPA but I need a way to have like record[].x where x is whatever variable the user decides the array should we sorted by.

我试图创建一个排序函数,它可以很好地排序,但是它一次只能处理一个变量比较。例如,我必须编写record [i] .GPA,以便比较两个记录的GPA。但是,我的老师希望这样做,以便函数可以根据任何字段进行排序。

I tried to create a sorting function and it sorts fine but it can only handle a single variable comparison at once. For example, I have to write record[i].GPA so it compares the GPAs of the two records. However, my teacher wants it so that the function can sort based on any of the fields.

template <class  T>
void sortArray(T record[]) {

    bool swap = true;
    while (swap) {
        swap = false;
        for (size_t i = 0; i < arraySize - 1; i++) {
            if (record[i].GPA< record[i + 1].GPA) {
                T temp = record[i];
                record[i] = record[i + 1];
                record[i + 1] = temp;
                swap = true;
            }
        }
    }
}

此如您所见,这是我的排序代码,在这种情况下,我必须指出要对GPA进行排序的记录变量,但是我需要根据用户的选择对它进行排序。我不认为每个变量都具有多个排序功能。如果需要,我可以发布其余代码。

This is my sorting code as you can see I had to indicate which record variable to sort by, GPA in this case, but I need it to sort by any of the record variables depending on user selection. I'm not suppose to have multiple sort functions for each variable. I can post the rest of my code if required.

推荐答案

您可能会做类似的事情:

You might do something like:

auto makeComp = [](auto member){
    return [=](const auto& lhs, const auto& rhs){
        return std::invoke(member, lhs) < std::invoke(member, lhs);
    }
};

switch (eMember)
{
    case EMember::FirstName: std::sort(record, record + arraySize, makeComp(T::FirstName)); break;
    case EMember::LastName:  std::sort(record, record + arraySize, makeComp(T::LastName)); break;
    case EMember::GPA:       std::sort(record, record + arraySize, makeComp(T::GPA)); break;
    case EMember::Id:        std::sort(record, record + arraySize, makeComp(T::Id)); break;
    case EMember::EMail:     std::sort(record, record + arraySize, makeComp(T::Email)); break;
}

这篇关于无论对象变量类型如何,如何对对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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