Sort by

recency

|

947 Discussions

|

  • + 1 comment

    Python3, tried this way

    from functools import cmp_to_key
    
    def compare_big(num1, num2):
        if len(num1) > len(num2):
            return 1
        elif len(num1) < len(num2):
            return -1
        else:
            for d1, d2 in zip(num1, num2):
                if d1 > d2:
                    return 1
                elif d1 < d2:
                    return -1
            return 0
    
    def bigSorting(unsorted):
        return sorted(unsorted, key=cmp_to_key(compare_big))
        # Write your code herefrom functools import cmp_to_key
    
    def compare_big(num1, num2):
        if len(num1) > len(num2):
            return 1
        elif len(num1) < len(num2):
            return -1
        else:
            for d1, d2 in zip(num1, num2):
                if d1 > d2:
                    return 1
                elif d1 < d2:
                    return -1
            return 0
    
    def bigSorting(unsorted):
        return sorted(unsorted, key=cmp_to_key(compare_big))
        # Write your code here
    

    B

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/GAvltofMdYc

    vector<string> bigSorting(vector<string> u) {
        sort(u.begin(), u.end(), [](string l, string r){
            if(l.size() == r.size()) return l < r;
            return l.size() < r.size();
        });
        return u;
    }
    
  • + 0 comments
    function bigSorting(unsorted) {
      return unsorted.sort((a, b) => {
        if (a.length !== b.length) {
          return a.length - b.length;
        }
    
        for (let i = 0; i < a.length; i++) {
          if (a[i] !== b[i]) return a[i] - b[i];
        }
      });
    }
    
  • + 0 comments
    def bigSorting(unsorted):
        return sorted(unsorted, key=lambda x: (len(x), x))
    
  • + 0 comments

    python3

    def bigSorting(unsorted):
        # Write your code here
        lengths = { len(i) for i in unsorted }
        s = []
        
        for l in sorted(list(lengths)):
            t = filter(lambda x: len(x) == l, unsorted)
            s += sorted(t)
            
        return s