Browsed by
Tag: sorting

Sort dictionary in Python

Sort dictionary in Python

Python dictionary stores data in key-value pairs. In some situations, programmers need to sort the dictionary object as per their requirement which may be based on key or value. To sort dictionary object, you can user sorted() method and pass either keys or values from the dictionary which sorts the elements in their natural order. For example, if the elements are all integers, then smaller numbers go earlier in the list. If the elements are all strings, they are arranged in alphabetic order. Below examples demonstrate different ways to sort the dictionary based on key or value.

Read More Read More

Sort list in Python

Sort list in Python

Python has different methods to sort elements of a list. These methods arrange elements in their natural order either in ascending or descending order. For example, if the elements are all integers, then smaller numbers go earlier in the list. If the elements are all strings, they are arranged in alphabetic order. Below are examples to sort list using list.sort(), sorted() method in ascending and descending order.

Read More Read More

Sort a Map in Java Example

Sort a Map in Java Example

A map is an data structure class that stores associations between keys and values which are widely used in java programming language. As map stores key-value pair, we can can sort map by two ways.

  1. Sort by keys
  2. Sort by values

Let us see example to sort map in both ways.

Read More Read More