简体   繁体   中英

Sort a List<string> comparing with a string

I need to sort a List<string> by comparing the list with a string

for example: I have a List that contains the following Items.

Kaboki
kriiki
Kale
Kutta
Kiki
Kicki
Krishna
Kseaki

The search keyword is ki I need to sort the list items using the keyword in such a way that, the strings that match in the string start have should be first and the string having the matched string in the other position have to be in the last

Here is my current code

    public static List<string> GetLocations(string prefixText)
    {
        try
        {
            DataTable dtlocs = (DataTable) HttpContext.Current.Session["locations"];
            var dValue = from row in dtlocs.AsEnumerable()
                         where row.Field<string>("Location_Name").ToLower().Contains(prefixText.ToLower())
                         select row.Field<string>("Location_Name");

            var results = dValue.OrderBy(s => s.IndexOf(prefixText, StringComparison.Ordinal));
            var str = new List<string>(results.ToList());

            if (!str.Any())
                str.Add("No locations found");
            return str;
        }
        catch (Exception)
        {
            var str = new List<string> {"No locations found"};
            return str;
        }
    }

Here I'm able to get the first matched values to the top but cannot sort the remaining values

and I have another issue. there is a word King Koti and i'm searhing for Ko and this word comes to first.I think this happens because, the string has two sub strings and one of the substrings start with the matched word.

and can I make the matched letters to bold ??

var res = list.OrderBy(y=> !y.StartsWith("Ki", StringComparison.OrdinalIgnoreCase))
              .ThenBy(x => x)

OrderBy orders false before true :

var result = list.OrderBy(s => !s.StartsWith("ki", StringComparison.OrdinalIgnoreCase))
                 .ThenBy(s => !s.ToLower().Contains("ki"));

You can use a combination of Linq's OrderBy and the IndexOf methods:

var input = ...
var search = "ki";
var results = input.Select(Value => new { Value, Index = s.IndexOf(search, StringComparison.InvariantCultureIgnoreCase) })
                   .Where(pair => pair.Index >= 0)
                   .OrderBy(pair => pair.Index)
                   .Select(pair => pair.Value);

Or in query syntax:

var results = 
   from s in input
   let i = s.IndexOf(search, StringComparison.InvariantCultureIgnoreCase)
   where i >= 0
   orderby i
   select s;

I think this should work:

list = (from str in list
        let idx = str.IndexOf(keyword, StringComparison.OrdinalIgnoreCase)
        let change = idx != 0 ? idx : int.MinValue
        orderby change
        select str).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