从排序另一个阵列为了一个阵列? [英] Sort One Array from Another array order?

查看:123
本文介绍了从排序另一个阵列为了一个阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var listOne = new string[] { "dog", "cat", "car", "apple"};
var listTwo = new string[] { "car", "apple"};

我需要的是通过项目在listTwo的顺序(如present)命令那么listOne。因此,新的名单将在这个秩序;
   汽车,苹果,狗,猫

What I need is to order listOne by the order of items in listTwo (if present). So the new list would be in this order; "car", "apple", "dog", "cat"

我想做到这一点的LINQ,并尝试这一点;

I would like to do this in LINQ and have tried this;

var newList = from l1 in listOne
              join l2 in listTwo on l1 equals l2 in temp
              from nl temp.DefaultIfEmpty()
              select nl;

但它返回null左右,显然我的LINQ福较弱。任何的建议是AP preciated。

But it returns null so evidently my Linq-Fu is weak. Any advise is appreciated.

推荐答案

您需要的所有从listTwo是在那么listOne后跟那么listOne余下项目的项目?

You need all the items from listTwo that are in listOne followed by the remaining items from listOne?

var results = listTwo.Intersect(listOne).Union(listOne);
foreach (string r in results)
{
    Console.WriteLine(r);
}

这篇关于从排序另一个阵列为了一个阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆