Refactoring is the technique of changing internal attributes without affecting the external attributes in an optimized manner. Bad smells lead to various problems in source code that further increase the requirement for refactoring. Initially, in this paper, prioritization of classes is done with the help of newly proposed metric called Quality Decline Factor (QDF), which takes into account an appropriate ratio of software metrics along with eleven types of bad smells detected. Further, bad smells are removed by applying refactoring techniques and change in metrics is observed. Afterwards, Machine learning algorithms are used to assign weights to each of the metrics and another new metric, Total Refactoring Index (TRI) is proposed. TRI takes combination of assigned weights and the effect of change in metrics to determine optimum refactoring sequence. Results show that the Decision Tree Forest algorithm is best suited to determine the refactoring sequence. After applying this technique, it was observed that 94.9% of the efforts were saved. This study would be beneficial for the software maintainers as beforehand sequences will be known. Further, their focus would only be centred on a limited portion of the code that contains more bad smells, hence completing the project within real-time constraints.