OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python array sorting price incorrectly

  • Thread starter Thread starter Slippy
  • Start date Start date
S

Slippy

Guest
I have made an array with a description and price but can't seem to get the sortign to sort by price. Can someone assist or guide me on why this isn't sorting by price correctly? I have checked the type fro price and it is a float. From what I understand, it should be working. I am not getting an error either. However the output is not sorted by price. :/

Build the array

Code:
import array 
newlist = []
for d in products:
    desc =d.description
    price=d.price
    #print(type(price))
    newlist.append(f"{desc}:{price}")

newlist output

Code:
['Coors Light American-style Light Lager Beer:16.99', 'Coors Light American-style Light Lager Beer:26.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:18.99', 'Samuel Adams Summer Ale Seasonal Beer:19.99', 'Miller Lite American Pilsner Light Lager Beer:22.99', 'Miller Lite American Pilsner Light Lager Beer:26.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:26.99', 'Bud Light Lager Beer:32.99', 'Samuel Adams Summer Ale Seasonal Beer:10.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:33.99', 'Busch Light Domestic Lager Beer:31.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:26.99', 'Coors Banquet American Lager Beer:16.99', 'Stella Artois Premium Lager Beer:20.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:34.99', "Leinenkugel's Summer Shandy Craft Beer:19.99", 'Coors Light American-style Light Lager Beer:22.99', 'Blue Moon Belgian-Style Ale White Wheat Beer:19.99', 'Blue Moon Belgian-Style Ale White Wheat Beer:19.99', 'Heineken Original Lager Beer, 12 Pack, 12 fl oz Bottles:20.99']

Try to sort by price

Code:
import operator
getcount = itemgetter(int(1))
list(map(getcount, newlist))
sortedlist = sorted(newlist, key=getcount)

for l in sortedlist:
    print(l)

Output

Code:
Samuel Adams Summer Ale Seasonal Beer:10.99
Leinenkugel's Summer Shandy Craft Beer:19.99
Heineken Original Lager Beer, 12 Pack, 12 fl oz Bottles:20.99
Michelob ULTRA Superior Light Domestic Lager Beer:18.99
Miller Lite American Pilsner Light Lager Beer:22.99
Miller Lite American Pilsner Light Lager Beer:26.99
Michelob ULTRA Superior Light Domestic Lager Beer:26.99
Michelob ULTRA Superior Light Domestic Lager Beer:33.99
Michelob ULTRA Superior Light Domestic Lager Beer:26.99
Michelob ULTRA Superior Light Domestic Lager Beer:34.99
Blue Moon Belgian-Style Ale White Wheat Beer:19.99
Blue Moon Belgian-Style Ale White Wheat Beer:19.99
Dos Equis Mexican Lager Beer, 12 Pack, 12 fl oz Bottles:19.99
Coors Light American-style Light Lager Beer:16.99
Coors Light American-style Light Lager Beer:26.99
Coors Banquet American Lager Beer:16.99
Coors Light American-style Light Lager Beer:22.99
Stella Artois Premium Lager Beer:20.99
Bud Light Lager Beer:32.99
Busch Light Domestic Lager Beer:31.99
<p>I have made an array with a description and price but can't seem to get the sortign to sort by price. Can someone assist or guide me on why this isn't sorting by price correctly? I have checked the type fro price and it is a float. From what I understand, it should be working. I am not getting an error either. However the output is not sorted by price. :/</p>
<p>Build the array</p>
<pre><code>import array
newlist = []
for d in products:
desc =d.description
price=d.price
#print(type(price))
newlist.append(f"{desc}:{price}")
</code></pre>
<p>newlist output</p>
<pre><code>['Coors Light American-style Light Lager Beer:16.99', 'Coors Light American-style Light Lager Beer:26.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:18.99', 'Samuel Adams Summer Ale Seasonal Beer:19.99', 'Miller Lite American Pilsner Light Lager Beer:22.99', 'Miller Lite American Pilsner Light Lager Beer:26.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:26.99', 'Bud Light Lager Beer:32.99', 'Samuel Adams Summer Ale Seasonal Beer:10.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:33.99', 'Busch Light Domestic Lager Beer:31.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:26.99', 'Coors Banquet American Lager Beer:16.99', 'Stella Artois Premium Lager Beer:20.99', 'Michelob ULTRA Superior Light Domestic Lager Beer:34.99', "Leinenkugel's Summer Shandy Craft Beer:19.99", 'Coors Light American-style Light Lager Beer:22.99', 'Blue Moon Belgian-Style Ale White Wheat Beer:19.99', 'Blue Moon Belgian-Style Ale White Wheat Beer:19.99', 'Heineken Original Lager Beer, 12 Pack, 12 fl oz Bottles:20.99']
</code></pre>
<p>Try to sort by price</p>
<pre><code>import operator
getcount = itemgetter(int(1))
list(map(getcount, newlist))
sortedlist = sorted(newlist, key=getcount)

for l in sortedlist:
print(l)
</code></pre>
<p>Output</p>
<pre><code>Samuel Adams Summer Ale Seasonal Beer:10.99
Leinenkugel's Summer Shandy Craft Beer:19.99
Heineken Original Lager Beer, 12 Pack, 12 fl oz Bottles:20.99
Michelob ULTRA Superior Light Domestic Lager Beer:18.99
Miller Lite American Pilsner Light Lager Beer:22.99
Miller Lite American Pilsner Light Lager Beer:26.99
Michelob ULTRA Superior Light Domestic Lager Beer:26.99
Michelob ULTRA Superior Light Domestic Lager Beer:33.99
Michelob ULTRA Superior Light Domestic Lager Beer:26.99
Michelob ULTRA Superior Light Domestic Lager Beer:34.99
Blue Moon Belgian-Style Ale White Wheat Beer:19.99
Blue Moon Belgian-Style Ale White Wheat Beer:19.99
Dos Equis Mexican Lager Beer, 12 Pack, 12 fl oz Bottles:19.99
Coors Light American-style Light Lager Beer:16.99
Coors Light American-style Light Lager Beer:26.99
Coors Banquet American Lager Beer:16.99
Coors Light American-style Light Lager Beer:22.99
Stella Artois Premium Lager Beer:20.99
Bud Light Lager Beer:32.99
Busch Light Domestic Lager Beer:31.99
</code></pre>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top