@Janvi149 you have to remove pagination for this solution to work. Because of how Shopify pulls the products for each page it will only sort each pagination page. You can try removing pagination and see how much it decreases your shop performance. Shopify still lazy loads the images so in my case I just removed the pagination to be able to sort the products properly.
I really think Shopify devs need to integrate custom sorting by custom meta fields as when we did our shop we moved from another site so the sort order was determined in the order of which products where imported and did not realize this until later down the line.
Hope that helps.