对矩形列表进行排序
本文关键字:排序 列表 | 更新日期: 2023-09-27 18:00:07
我想按照单词在页面上的出现顺序列出一个单词列表。(如果是这样的话,包括多条线路)
the quick brown
fox jumped
我有一个自定义Word
对象的列表,其中包含单词的文本,它的左、右、上和下值,(0,0)是页面的左上角。
words.Add(new Word() { text = "the", box = new rectangle() { left = 10, right = 30, top = 10, bottom = 21 } );
words.Add(new Word() { text = "brown", box = new rectangle() { left = 65, right = 95, top = 11, bottom = 20 } );
words.Add(new Word() { text = "jumped", box = new rectangle() { left = 36, right = 64, top = 26, bottom = 38 } );
words.Add(new Word() { text = "quick", box = new rectangle() { left = 35, right = 60, top = 11, bottom = 24 } );
words.Add(new Word() { text = "fox", box = new rectangle() { left = 10, right = 30, top = 25, bottom = 35 } );
internal class Word
{
internal Rectangle box { get; set; }
internal string text { get; set; }
}
我可以通过左界排序很容易地对一行进行排序,但两行会伤害我的大脑。
在LINQ中使用OrderBy
,然后使用ThenBy
,这将按X位置排序,然后按Y位置排序。
List<Word> sortedWords = words.OrderBy(w => w.box.Left).ThenBy(w => w.box.Top).ToList();