GVKun编程网logo

Python列表按降序排列(python列表降序排列怎么操作)

17

本篇文章给大家谈谈Python列表按降序排列,以及python列表降序排列怎么操作的知识点,同时本文还将给你拓展android–如何按降序排列listview项目、c#–可降序范围按降序排列、C程序:

本篇文章给大家谈谈Python列表按降序排列,以及python列表降序排列怎么操作的知识点,同时本文还将给你拓展android – 如何按降序排列listview项目、c# – 可降序范围按降序排列、C程序:按降序排列四个整数的输出、ios – 如何按降序排列日期数组等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Python列表按降序排列(python列表降序排列怎么操作)

Python列表按降序排列(python列表降序排列怎么操作)

如何解决Python列表按降序排列?

在一行中,使用lambda

timestamp.sort(key=lambda x: time.strptime(x, ''%Y-%m-%d %H:%M:%s'')[0:6], reverse=True)

将函数传递给list.sort

def foo(x):
    return time.strptime(x, ''%Y-%m-%d %H:%M:%s'')[0:6]

timestamp.sort(key=foo, reverse=True)

这将为你提供阵列的排序版本。

sorted(timestamp, reverse=True)

如果要就地排序:

timestamp.sort(reverse=True)

解决方法

如何按降序对列表进行排序?

timestamp = [
    "2010-04-20 10:07:30","2010-04-20 10:07:38","2010-04-20 10:07:52","2010-04-20 10:08:22","2010-04-20 10:09:46","2010-04-20 10:10:37","2010-04-20 10:10:58","2010-04-20 10:11:50","2010-04-20 10:12:13","2010-04-20 10:25:38"
]

android – 如何按降序排列listview项目

android – 如何按降序排列listview项目

所以我有一个listview,我想按降序排序NumberOfRecords.我有一个自定义数组适配器,但我在将数据放入ArrayList之前调用了我的排序类,这是我的Asynctask接收JSON:

public class SampleListTask extends AsyncTask<String, Void, String> {

    public ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(SampleActivity.this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... path) {
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

        Log.d(Constant.TAG_RANKING, path[0]);
        String apiRequestReturn = UtilWebService.getRequest(path[0]);
        if (apiRequestReturn.equals("")) {
            Log.d(Constant.TAG_SAMPLE, "WebService request is null");
            return null;
        } else {
            Log.d(Constant.TAG_SAMPLE, "WebService request has data");
            return apiRequestReturn;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }

        if (null == result || result.length() == 0) {
            application.shortToast("No data found from server");
        } else {
            try {
                JSONObject sampleObject = new JSONObject(result);
                JSONArray jsonArray = sampleObject
                        .getJSONArray(Constant.TAG_SAMPLE);

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject objJson = jsonArray.getJSONObject(i);

                    sample = new ArraySample();

                    sample.setId(objJson.getInt(Constant.TAG_SONGID));
                    sample.setThumbUrl(objJson
                            .getString(Constant.TAG_IMAGEURL));
                    sample.setTitle(objJson
                            .getString(Constant.TAG_NAME));
                    sample.setArtist(objJson
                            .getString(Constant.TAG_ARTIST));
                    sample.setDuration(Utility
                            .changeStringTimeFormat(objJson
                                    .getString(Constant.TAG_MUSICLENGTH)));
                    sample.setNumberOfRecords(objJson
                            .getString(Constant.TAG_NUMBEROFRECORDS));

                    Collections.sort(sampleList, new SortByRecordNumber()); // This where I call the class
                    sampleList.add(sample);
                }
            } catch (JSONException e) {
                e.printstacktrace();
            }
            setAdapterToListview();
        }
    }

    public void setAdapterToListview() {
        objRowAdapter = new RowAdapterSample(getApplicationContext(),
                R.layout.item_sample, sampleList);
        sampleListView.setAdapter(objRowAdapter);
    }
}

这是我的排序类:

public class SortByRecordNumber implements Comparator {

public int compare(Object o1, Object o2) {
    ArraySample p1 = (ArraySample) o1;
    ArraySample p2 = (ArraySample) o2;

    return p2.getNumberOfRecords().compareto(p1.getNumberOfRecords());
}

}

但我得到的结果是:

5
15
14
0
0

我的排序实现错了吗?或者我应该在返回之前将其解析为Integer?

解决方法:

您可以使用以下代码对整数列表进行降序排序.这里我们重写compare(),以便按降序排序.

//sort list in desc order 
            Collections.sort(myIntegerList, new Comparator<Integer>() {
                public int compare(Integer one, Integer other) {
                     if (one >= other) {
                         return -1;
                     } else {
                         return 1;
                     } 
                   }
            });

希望能帮助到你.

c# – 可降序范围按降序排列

c# – 可降序范围按降序排列

我使用enumerable.range()绑定一个组合框,它工作正常.
现在我试图按降序显示结果,我该怎么做?
cboYearList.ItemsSource = Enumerable.Range( DateTime.Today.Year,1950).ToList().OrderByDescending();

解决方法

使用Enumerable.Range创建列表后,可以使用 Reverse列表:
cboYearList.ItemsSource = Enumerable.Range(DateTime.Today.Year,1950).Reverse().ToList();

或者如果你想保留你的OrderByDescending,你需要传递一个键选择器(i => i):

cboYearList.ItemsSource = Enumerable.Range( DateTime.Today.Year,1950).OrderByDescending(i => i).ToList();

C程序:按降序排列四个整数的输出

C程序:按降序排列四个整数的输出

一、编写c程序对输入的四个整数按从大到小的顺序输出?

#include <stdio.h>

int main() {
    int num1, num2, num3, num4;

    // 输入四个整数
    printf("请输入四个整数:\n");
    scanf("%d %d %d %d", &num1, &num2, &num3, &num4);

    // 对输入的四个整数进行排序
    if (num1 < num2) {
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }
    if (num1 < num3) {
        int temp = num1;
        num1 = num3;
        num3 = temp;
    }
    if (num1 < num4) {
        int temp = num1;
        num1 = num4;
        num4 = temp;
    }
    if (num2 < num3) {
        int temp = num2;
        num2 = num3;
        num3 = temp;
    }
    if (num2 < num4) {
        int temp = num2;
        num2 = num4;
        num4 = temp;
    }
    if (num3 < num4) {
        int temp = num3;
        num3 = num4;
        num4 = temp;
    }

    // 输出排序后的结果
    printf("按从大到小的顺序输出:%d %d %d %d\n", num1, num2, num3, num4);

    return 0;
}
登录后复制

二、随机输入4个整数按从大到小输出用条件语句编程?

在输入情况随机的情况下,我们可以使用条件语句来进行排序。下面是一个示例:

#include <stdio.h>

int main() {
    int num1, num2, num3, num4;

    // 模拟随机输入四个整数
    printf("模拟随机输入四个整数:\n");
    scanf("%d %d %d %d", &num1, &num2, &num3, &num4);

    // 使用条件语句进行排序
    if (num1 < num2) { int temp = num1; num1 = num2; num2 = temp; }
    if (num1 < num3) { int temp = num1; num1 = num3; num3 = temp; }
    if (num1 < num4) { int temp = num1; num1 = num4; num4 = temp; }
    if (num2 < num3) { int temp = num2; num2 = num3; num3 = temp; }
    if (num2 < num4) { int temp = num2; num2 = num4; num4 = temp; }
    if (num3 < num4) { int temp = num3; num3 = num4; num4 = temp; }

    // 输出排序后的结果
    printf("按从大到小的顺序输出:%d %d %d %d\n", num1, num2, num3, num4);

    return 0;
}
登录后复制

三、C#输入四个整数从大到小输出?

using System;

class Program {
    static void Main() {
        Console.WriteLine("请输入四个整数,用空格分隔:");
        string[] input = Console.ReadLine().Split(&#39; &#39;);

        // 将输入的字符串转换为整数数组
        int[] numbers = new int[input.Length];
        for (int i = 0; i < input.Length; i++) {
            numbers[i] = int.Parse(input[i]);
        }

        // 使用Array.Sort方法对整数数组进行排序
        Array.Sort(numbers);

        // 按从大到小的顺序输出
        Console.WriteLine("按从大到小的顺序输出:{0} {1} {2} {3}", numbers[3], numbers[2], numbers[1], numbers[0]);
    }
}
登录后复制

总结:

以上是使用C和C#编写的三个程序,分别演示了手动输入四个整数排序和随机输入四个整数排序的方法。C#中使用了内置的Array.Sort方法对整数数组进行排序。排序的思路都是通过比较大小然后交换位置,最终输出按从大到小的顺序。

以上就是C程序:按降序排列四个整数的输出的详细内容,更多请关注php中文网其它相关文章!

ios – 如何按降序排列日期数组

ios – 如何按降序排列日期数组

我有一个NSDictionary解析到一个数组的元素之一是日期,我尝试使用[startimeArray sortUsingSelector:@selector(compare :)]; (starttimeArray)是我的日期,但只安排升序.我如何按降序排列?谢谢

解决方法

通过put NO排序数组是升序参数:
NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverSEOrder=[dateArray sortedArrayUsingDescriptors:descriptors];

关于Python列表按降序排列python列表降序排列怎么操作的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于android – 如何按降序排列listview项目、c# – 可降序范围按降序排列、C程序:按降序排列四个整数的输出、ios – 如何按降序排列日期数组的相关知识,请在本站寻找。

本文标签: