def sort(self,key='start'):
        """Sort the bed according to the key.
        
        Parameters:
        1. key: the key according to which the bed is sorted (default='start')"""

        # iterate through the choromosomes
        chroms=self.get_chroms()
        
        for chrom in chroms:
            try:
                key_column=self.bed[chrom][key]
            except KeyError, e:
                raise Exception('%s is not a valid column name' %(e))
            
            # if key_column is not list, TypeError is raised. Then, convert key_column into a list and do sorting again.
            try:
                sort_order=self.__argsort(key_column)
            except TypeError:
                key_column=list(key_column)
                sort_order=self.__argsort(key_column)
            
            # sort the other columns according to sort_order
            try:
                columns=self.bed[chrom].keys()
            except AttributeError:
                raise Exception('Empty bed in %s' %(chrom))
            
            for column in columns:
                if column=='chrom': continue
                if column=='start' or column=='end' or column=='thickStart' or column=='thickEnd' or column=='blockCount':
                    self.bed[chrom][column]=array('l',[self.bed[chrom][column][so] for so in sort_order])
                elif column=='score':
                    self.bed[chrom][column]=array('d',[self.bed[chrom][column][so] for so in sort_order])
                else:
                    self.bed[chrom][column]=[self.bed[chrom][column][so] for so in sort_order]
dataset/ETHPy150Open taoliu/taolib/Assoc/inout.py/Bed.sort/original