gpt4 book ai didi

c# 多条件排序

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:18 24 4
gpt4 key购买 nike

请耐心等待,我会尽量不让这件事变得太复杂。我有一份产品 list 。一个产品看起来像这样:

{
"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
}

但为了帮助解释这一点,我将对其进行简化。我正在尝试创建一个允许对多列进行排序的排序方法。比如我有这个方法:

/// <summary>
/// Performs an advanced sort on products using sortations
/// </summary>
/// <param name="products">The products to sort</param>
/// <param name="sortations">The sortation list</param>
public void AdvancedSort(List<JObject> products, List<Sortation> sortations)
{

// Get our value types
GetTypesFromOperator(sortations);

// Sort our products by the sortations
products.Sort((a, b) =>
{
foreach (var sortation in sortations)
{

// Get our values
var fieldName = sortation.Field;
var x = a.SelectToken(fieldName).ToString();
var y = b.SelectToken(fieldName).ToString();
var type = sortation.Type;

// If both values are the same, skip the rest of this iteration
if (x.Equals(y)) return 0;

// If we have an expression
if (!string.IsNullOrEmpty(sortation.Expression))
{

// If we are checking for between
if (sortation.Operator == "><")
{

// Get our values
var values = sortation.Expression.Split(',');

// If we have 2 values
if (values.Length == 2)
return CompareBetween(values, x, y);
}

// If we are checking booleans
if (sortation.Operator == "===")
return CompareBoolean(sortation.Expression);

// If we are checking equals
if (sortation.Operator == "=")
return y.Equals(sortation.Expression) ? 1 : -1;

// If we are checking like
if (sortation.Operator == "%")
return y.Equals(sortation.Expression, StringComparison.OrdinalIgnoreCase) ? 1 : -1;
}

// If we get this far, do a switch on sortation types
switch (sortation.Type)
{
case SortationValueType.Boolean:
return Boolean.Parse(x) ? -1 : 1;
case SortationValueType.Number:
return CompareNumeric(x, y, sortation.Direction);
default:
return string.CompareOrdinal(x, y);
}
}

// If nothing matches, return 0
return 0;
});
}

它试图根据属性值类型进行排序,我称之为排序排序只是一个类,它规定了要查询的运算符、表达式和字段。这是一个排序的例子:

// Add importance, expert and sponsored
sortations.Add(new Sortation
{
Field = "importance",
Operator = "<"
});

现在,我们可以有许多动态添加的不同分类。我需要能够通过这些分类对产品进行分类。我创建了一个简单的测试:

[Test]
public void SortBySilverColourSilverShouldBeTop()
{

// Assemble
var json = "[{ colour: 'Black', importance: '150' }, { colour: 'Black', importance: '150' }, { colour: 'Black', importance: '150' }, { colour: 'Silver', importance: '150' }, { colour: 'Black', importance: '149' }, { colour: 'Black', importance: '149' }, { colour: 'Black', importance: '149' }, { colour: 'Silver', importance: '149' }]";
var products = JsonConvert.DeserializeObject<List<JObject>>(json);
var sortations = new List<Sortation>() { new Sortation() { Field = "colour", Operator = "=", Expression = "Silver" }, new Sortation() { Field = "importance", Operator = "<" } };

// Act
_sortProvider.AdvancedSort(products, sortations);
var result = products[0].SelectToken("colour").ToString();

// Assert
Assert.That(result, Is.EqualTo("Silver"));
}

我希望得到这样的输出:

[  
{ colour:'Silver', Importance:'150'},
{ colour:'Black', Importance:'150' },
{ colour:'Black', Importance:'150' },
{ colour:'Black', Importance:'150' },
{ colour:'Silver', Importance:'149' },
{ colour:'Black', Importance:'149' },
{ colour:'Black', Importance:'149' },
{ colour:'Black', Importance:'149' }
]

但我得到的是这个输出:

[  
{ colour:'Silver', Importance:'150' },
{ colour:'Silver', Importance:'149' },
{ colour:'Black', Importance:'150' },
{ colour:'Black', Importance:'150' },
{ colour:'Black', Importance:'150' },
{ colour:'Black', Importance:'149' },
{ colour:'Black', Importance:'149' },
{ colour:'Black', Importance:'149' }
]

谁能帮我解决我的排序方法?

最佳答案

在我看来,您的基本问题是,一旦第一个排序命中匹配项,它就会将排序顺序值返回给排序函数,而其余排序根本无关紧要。我相信您需要做的是交换 foreach 和 products.sort 调用,如下所示:

// Get our value types
GetTypesFromOperator(sortations);

foreach (var sortation in sortations)
{
// Sort our products by the sortations
products.Sort((a, b) =>
{
// Get our values
var fieldName = sortation.Field;
var x = a.SelectToken(fieldName).ToString();
var y = b.SelectToken(fieldName).ToString();
var type = sortation.Type;
....
....
}
}

这将花费更长的时间,但会考虑每个分类。请记住,列表中最后的排序将是最重要的。

关于c# 多条件排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44329681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com