sorting list?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicstel
    New Member
    • Jun 2008
    • 20

    sorting list?

    I'm trying to find a way to sort a list..

    Code:
    list =  ["B1", "B2", "B10", "C1", "C2", "VB1", "VB2", "VB15"]
    list.sort()
    list.sort() give me: ['B1', 'B10', 'B2', 'C1', 'C2', 'VB1', 'VB15', 'VB2']
    I want this: ['B1', 'B2', 'B10', 'C1', 'C2', 'VB1', 'VB2', 'VB15']

    Do I need to use sting.split or something like that?

    Thank You
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You should not use "list" as an identifier. The built-in function list() will be masked and inaccessible until the object is deleted.

    You will have to pass a function to sort() sort your list in that manner. This is overkill for your example, but I use it for sorting material piecemarks:
    Code:
    import re
    
    def sortMk(a, b):
        # Sort piece marks
        # Example mark list: [BP1, BP33, BP2, a123, a2, 901T1, p1002, p12, p456, w77, w211]
        # Sorted: ['901T1', 'BP1', 'BP2', 'BP33', 'a2', 'a123', 'p12', 'p456', 'p1002', 'w77', 'w211']
        
        pattA = re.compile('(^[a-z,A-Z]+)')
        pattD = re.compile('(\d+)')
        pattE = re.compile('(\d+$|\d+[a-zA-Z]+$)')
        pattP = re.compile('([a-zA-Z]+)')
        
        try:
            x = cmp(pattA.search(a).group(1), pattA.search(b).group(1))
        except:
            try:
                x = cmp(int(pattD.match(a).group(1)), int(pattD.match(b).group(1)))
            except:
                return cmp(a, b)
            if not x:
                try:
                    x = cmp(pattP.search(a).group(1), pattP.search(b).group(1))
                except:
                    return cmp(a, b)
            if not x:
                try:
                    return cmp(int(pattE.search(a).group(1)), int(pattE.search(b).group(1)))
                except:
                    try:
                        return cmp(pattE.search(a).group(1), pattE.search(b).group(1))
                    except:
                        return cmp(a, b)
        if not x:
            try:
                return cmp(int(pattD.search(a).group(1)), int(pattD.search(b).group(1)))
            except:
                return cmp(a, b)
        return x
    
    mylist =  mylist =  ['B1', 'B10', 'B2', 'C1', 'C2', 'VB1', 'VB15', 'VB2', 'DEFG4775', 'DEFG12']
    mylist.sort(sortMk)
    print mylist
    The output:
    Code:
    >>> ['B1', 'B2', 'B10', 'C1', 'C2', 'DEFG12', 'DEFG4775', 'VB1', 'VB2', 'VB15']
    >>>

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      This stripped down version should work for you:
      Code:
      import re
      def sortMk(a, b):
          pattA = re.compile('(^[a-z,A-Z]+)([0-9]+$)')
          m1= pattA.search(a)
          a1 = m1.group(1), int(m1.group(2))
          m2= pattA.search(b)
          b1 = m2.group(1), int(m2.group(2))
          return cmp(a1, b1)

      Comment

      • nicstel
        New Member
        • Jun 2008
        • 20

        #4
        It works perfectly !! it's more complicated as I thought, but I like it.

        Thanks BV

        Comment

        Working...