I have a number of shopping lists ordered by pick order. Now I'd like to merge all the lists keeping the sort order resulting in a list ordered by the "perfect" pick order for my next shopping occasion. How do I do that?
Example:
list A: Toothpaste, Bread, Meat, Vegetables, Milk, Ice cream list B: CDs开发者_开发问答, Bread, Fruit, Vegetables, Juice, Sugar, Chocolates list C: Meat, Juice, Milk, SugarWould result in:
(Toothpaste, CDs), Bread, Meat, Fruit, Vegetables, Juice, Milk, Sugar, (Ice cream, Chocolates) Items within parentheses have unknown sort order within the parentheses.You should try Topological sorting. This is a sorting used when you have a partial order (Without cycles) defined on a set of items. From the initial lists you will build some partial order pairs and use those pair inside the topological sorting algorithm.
For example you would extract the following info from your initial lists:
- Toothpaste < Bread
- Bread < Meat
- Meat < Vegetables
- Vegetables < Milk
- Milk < Ice Cream
- CDs < Bread
- etc.
If you don't get a cycle (aka A < B and B < A ) you will be able to construct a "good" ordering of all items.
精彩评论