简体   繁体   中英

Sort list of rectangles

I want to put a list of words in order of how they appeared on the page they were found. (Including multiple lines if that is the case)

the quick brown
fox jumped

I have a list of custom Word objects that contain the text of the word, and it's left, right, top, and bottom values with (0, 0) being the top left corner of the page.

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; }
}

I can easily sort a single line by ordering by the left bound, but 2 lines is hurting my brain.

LINQ中使用OrderBy然后ThenBy ,它将按X位置排序,然后按Y位置排序。

List<Word> sortedWords = words.OrderBy(w => w.box.Left).ThenBy(w => w.box.Top).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM