📌  相关文章
📜  根据字符串长度排序字符串数组

📅  最后修改于: 2021-04-29 05:34:53             🧑  作者: Mango

给定一个字符串数组,我们需要按字符串长度的升序对数组进行排序。
例子:

Input : {"GeeksforGeeeks", "I", "from", "am"}
Output : I am from GeeksforGeeks

Input :  {"You", "are", "beautiful", "looking"}
Output : You are looking beautiful

一个简单的解决方案是编写我们自己的排序函数,该函数比较字符串长度以确定哪个字符串应该排在最前面。下面是使用插入排序对数组进行排序的实现。

C++
// C++ program to sort an Array of
// Strings according to their lengths
#include
using namespace std;
 
// Function to print the sorted array of string
void printArraystring(string,int);
 
// Function to Sort the array of string
// according to lengths. This function
// implements Insertion Sort.
void sort(string s[], int n)
{
    for (int i=1 ;i= 0 && temp.length() < s[j].length())
        {
            s[j+1] = s[j];
            j--;
        }
        s[j+1] = temp;
    }
}
  
// Function to print the sorted array of string
void printArraystring(string str[], int n)
{
    for (int i=0; i


Java
// Java program to sort an Array of
// Strings according to their lengths
import java.util.*;
 
class solution
{
 
// Function to print the sorted array of string
// void printArraystring(string,int);
 
// Function to Sort the array of string
// according to lengths. This function
// implements Insertion Sort.
static void sort(String []s, int n)
{
    for (int i=1 ;i= 0 && temp.length() < s[j].length())
        {
            s[j+1] = s[j];
            j--;
        }
        s[j+1] = temp;
    }
}
 
// Function to print the sorted array of string
static void printArraystring(String str[], int n)
{
    for (int i=0; i


Python3
# Python3 program to sort an Array of
# Strings according to their lengths
 
# Function to print the sorted array of string
def printArraystring(string, n):
    for i in range(n):
        print(string[i], end = " ")
 
# Function to Sort the array of string
# according to lengths. This function
# implements Insertion Sort.
def sort(s, n):
    for i in range(1, n):
        temp = s[i]
 
        # Insert s[j] at its correct position
        j = i - 1
        while j >= 0 and len(temp) < len(s[j]):
            s[j + 1] = s[j]
            j -= 1
 
        s[j + 1] = temp
 
# Driver code
if __name__ == "__main__":
    arr = ["GeeksforGeeks", "I", "from", "am"]
    n = len(arr)
 
    # Function to perform sorting
    sort(arr, n)
 
    # Calling the function to print result
    printArraystring(arr, n)
 
# This code is contributed by
# sanjeev2552


C#
// C# program to sort an Array of
// Strings according to their lengths
using System;
  
public class solution{
 
    // Function to print the sorted array of string
    // void printArraystring(string,int);
 
    // Function to Sort the array of string
    // according to lengths. This function
    // implements Insertion Sort.
    static void sort(String []s, int n)
    {
        for (int i=1 ;i= 0 && temp.Length < s[j].Length)
            {
                s[j+1] = s[j];
                j--;
            }
            s[j+1] = temp;
        }
    }
 
    // Function to print the sorted array of string
    static void printArraystring(String []str, int n)
    {
        for (int i=0; i


CPP
#include 
using namespace  std;
 
// Function to check the small string
bool compare(string &s1,string &s2)
{
    return s1.size() < s2.size();
}
 
// Function to print the sorted array of string
void printArraystring(string str[], int n)
{
    for (int i=0; i


Python3
# Python code for the above approach
def printsorted(arr):
   
    # Sorting using sorted function
    # providing key as len
    print(*sorted(arr, key=len))
 
 
# Driver code
arr = ["GeeksforGeeks", "I", "from", "am"]
 
# Passing list to printsorted function
printsorted(arr)
 
# this code is contributed by vikkycirus


输出
I am from GeeksforGeeks 

更好的解决方案是使用C++, Java之类的编程语言提供的排序函数。这些功能还使我们能够编写自己的自定义比较器。下面是使用C++ STL Sort函数的C++实现。

CPP

#include 
using namespace  std;
 
// Function to check the small string
bool compare(string &s1,string &s2)
{
    return s1.size() < s2.size();
}
 
// Function to print the sorted array of string
void printArraystring(string str[], int n)
{
    for (int i=0; i
输出
I am from GeeksforGeeks 

方法#2:在Python使用sorted()函数的简化解决方案

  1. 以字符串作为列表。
  2. 通过提供键len在Python使用已排序的函数。

下面是实现:

Python3

# Python code for the above approach
def printsorted(arr):
   
    # Sorting using sorted function
    # providing key as len
    print(*sorted(arr, key=len))
 
 
# Driver code
arr = ["GeeksforGeeks", "I", "from", "am"]
 
# Passing list to printsorted function
printsorted(arr)
 
# this code is contributed by vikkycirus
输出
I am from GeeksforGeeks