按2个属性对模型列表进行排序

时间:2017-06-01 23:23:34

标签: c# sorting

我正在尝试对这样的对象列表进行排序:

products.Sort((a, b) =>
{
    var x = string.CompareOrdinal(a.SelectToken("model").ToString(), b.SelectToken("model").ToString());
    var z = string.CompareOrdinal(b.SelectToken("colour").ToString(), colour);
    return x > 0 ? z : x;
});

然后我想反向遍历列表,这样我就可以删除重复项:

// Loop through products in reverse
for (var i = products.Count() - 1; i >= 0; i--)
{

    // Get our product
    var product = products[i];

    // Get our product colour
    var productColour = product.SelectToken("colour").ToString();

    // If our colour is black
    if (colour == "Black")
    {

        // If the product colour is not black, remove the duplicate product
        if (!productColour.Equals(colour))
            products.RemoveAt(i);

    }
    else
    {

        // Get our product model
        var productModel = product.SelectToken("model").ToString();

        // If we are the same model but a different colour, remove the duplicate
        if (productModel.Equals(model) && !productColour.Equals(colour))
            products.RemoveAt(i);

        // Update our variable
        model = productModel;
    }
}

但它并非如此简单。 颜色变量默认为" Black"但可以真正设置为任何颜色。所以,我想首先按模型排序,然后按颜色进行排序,但是我希望它能够排序这样的颜色。如果颜色是" Pink"列表应该是这样的:

[{
    colour: 'Any'
},{
    colour: 'Any'
},{
    colour: 'Any'
},{
    colour: 'Black'
},{
    colour: 'Black'
},{
    colour: 'Pink'
}]

所以基本上,如果选择的颜色不是黑色,它应该在排序的底部,然后直接跟着黑色(所有其他颜色都在这两个之前)。如果选择的颜色为黑色,则黑色应位于列表的底部。 有人可以帮我解决这个问题吗?

仅供参考,这是将要排序的项目之一的示例:

{
    "id": 3797,
    "title": "Canon EOS 100D Digital SLR Camera with 18-55 IS STM Lens, HD 1080p, 18MP, 3\" LCD Touch Screen",
    "shortTitle": "Canon EOS 100D Black",
    "brand": "Canon",
    "model": "EOS 100D",
    "colour": "Black",
    "gtin": "8714574602721",
    "image": "http://piiick.blob.core.windows.net/images/Canon-EOS-100D-18-55-Black-8714574602721.png",
    "type": "Digital SLR",
    "lensFocalLength": "18-55",
    "lensType": "IS STM",
    "lensMount": "EF/EF-S",
    "maxAperture": "999",
    "connectivity": "",
    "shootingModes": "Scene Intelligent Auto (Stills and Movie), No Flash, Creative Auto, Portrait, Landscape, Close-up, Sports, SCN(Kids, Food, Candlelight, Night Portrait, Handheld Night Scene, HDR Backlight Control), Program AE , Shutter priority AE, Aperture priority AE, Manual (Stills and Movie)",
    "weight": 410.0,
    "width": 116.8,
    "height": 90.7,
    "depth": 69.4,
    "digitalZoom": "N/A",
    "opticalZoom": "N/A",
    "waterproof": false,
    "maxVideoResolution": "1920 x 1080",
    "sensorType": "CMOS",
    "sensorSize": "22.3 x 14.9 mm",
    "continuousShootingSpeed": "4",
    "iso": "1600",
    "style": "traditional",
    "designer": "",
    "dateAnnounced": "10/06/2008",
    "focusPoints": 7
}

产品可能没有黑色或所选颜色,但我不想将其从我的列表中删除(只是重复的模型)。

2 个答案:

答案 0 :(得分:1)

您的排序算法应该更改,以符合您的要求。 我理解的是你按模型排序然后你在相同的模型中按颜色排序,但selectedColor应该在底部。但是,黑色应该在所选颜色的正上方。我认为删除重复不应该是一个问题。

如有必要,交换1和-1。

products.Sort((a, b) =>
{
    var x = string.CompareOrdinal(a.SelectToken("model").ToString();
If (x!=0) return x;


if (a.color == selectedColor){
    return 1 
}

if (b.color == selectedColor){
    return -1
}

if (a.color == "black") {
    return 1
}

if (b.color == "black") {
    return -1
}
    return string.CompareOrdinal(b.SelectToken("colour").ToString(), colour);
});

答案 1 :(得分:0)

如果您打算删除重复内容,为什么不使用groupby?

string str = @"[{'model':'aaa', 'colour':'red'}
            ,{'model':'aaa', 'colour':'red'}
            ,{'model':'aaa', 'colour':'green'}
            ,{'model':'bbb', 'colour':'red'}]";

JArray allProducts = JArray.Parse(str);

//Get the first product of each group
var uniq = allProducts
    .GroupBy(x => new { model = x["model"], colour = x["colour"] })
    .Select(x => x.First())
    .ToList();

或按所选颜色过滤,添加where子句

string colour = "red";

var uniq = allProducts
    .GroupBy(x => new { model = x["model"], colour = x["colour"] })
    .Select(x => x.First())
    .Where(x => x["colour"].Value<string>() == colour)
    .ToList();