排序字典<string,list <int =“”>&gt;基于价值

时间:2016-11-12 10:51:45

标签: c# sorting dictionary

我有一个字符串作为键的字典和一个有序的整数列表List作为值;是否可以根据c#中的值顺序对其进行排序?

例如: myDict:

{
    test: /\.scss$/,
    exclude: /node_modules/,
    loaders: ['raw-loader', 'sass-loader']
}

排序为:

{
    "hp",<10,14,23>
     "dell", <6,8,9,10>
     "asus", <6,9,18>
     "mac", <7,98>
}

以下是我的尝试:

{
       "dell", <6,8,9,10>
        "asus", <6,9,18>
        "mac", <7,98>
        "hp",<10,14,23>
 }

假设要进行排序的部分:

//this creates a dictionary with requirements stated above for testing
Dictionary<string, List<int>> myDict = new Dictionary<string, List<int>
            >();


        var numbers = "8,13,16,21,24,25,31,33,36,63,66,70,76,82,94".Split(',').Select(Int32.Parse).ToList();
        myDict .Add("asus", numbers);

        numbers = "6,84,90,99".Split(',').Select(Int32.Parse).ToList();
        myDict .Add("dell", numbers);

        numbers = "10,11,20,21,23,26,28,29,31,38,39,40,50,52,61,65,66,70,75,94".Split(',').Select(Int32.Parse).ToList();
        myDict.Add("hp", numbers);

        numbers = "4,17,42,56,62,79,80".Split(',').Select(Int32.Parse).ToList();
        myDict .Add("mac",numbers );            

上面给出了错误&#34;至少有一个对象必须实现IComparable。&#34; 我也尝试将列表转换为字符串并执行以下操作:

var orderedDictionary = myDict.OrderByDescending(pairs => pairs.Value);

上述方法有效,但它将数字视为字符串;因此 10,85 会出现在 8,6 之前我猜它是因为&#34;,&#34; ASCII表示高于数字。

如何在c#中对带有排序整数列表的字典进行排序?或者通过检查其他单元格上的每个单元格来进行手动排序的唯一方法是什么?

1 个答案:

答案 0 :(得分:1)

您应该为<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_users_feedback" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.myapp.app.usersreviews.reviews" android:background="@drawable/users_bg" tools:showIn="@layout/activity_user_reviews"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:isScrollContainer="false" android:id="@+id/scroll_view" android:layout_below="@+id/sv_users" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- USER DATA--> <include layout="@layout/users_data_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/layout_data" /> <!-- TWITTER--> <TextView android:id="@+id/textView8" android:layout_width="match_parent" android:layout_height="33.62dp" android:background="#054F92" android:gravity="center_vertical" android:text="\@tweets" android:textColor="@android:color/white" android:textSize="16sp" android:paddingLeft="15dp" android:textStyle="normal|bold" /> <fragment android:id="@+id/cart_twitter_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="-50dp" android:nestedScrollingEnabled="true" class="com.myapp.app.TwitterFragment" tools:layout="@layout/fragment_twitter" /> <!-- TWITTER --> </LinearLayout> </ScrollView> </RelativeLayout>

实施IComparer<T>
List<int>

并使用LINQ OrderBy的this重载:

public MyListComparer : IComparer<List<int>>
{
    public int Compare(List<int> x, List<int> y)
    {
        var minLength = x.Count < y.Count ? x.Count : y.Count;
        for (var i = 0 ;i < minLength; i++)
        {
            if (x[i] > y[i])
            {
                return 1;
            }
            if (x[i] < y[i])
            {
                return -1;
            }
        }
        if (x.Count > y.Count)
        {
            return 1;
        }
        if (y.Count > x.Count)
        {
            return -1;
        }
        return 0;
    }
}
相关问题