I used world_borders.shp to test this.
To get features and print a slice of first 6 elements. The shapefile has 3784 records in the attributes table (many of them duplicates).
wb=iface.activeLayer()
iter = wb.getFeatures()
attr=[]
for feature in iter:
attr.append(feature.attributes())
print len(attr)
3784
print attr[0:5]
[[1.0, u'AA', u'Aruba', 193.0, 71218.0],
[2.0, u'AC', u'Antigua and Barbuda', 443.0, 68320.0],
[2.0, u'AC', u'Antigua and Barbuda', 443.0, 68320.0],
[4.0, u'AG', u'Algeria', 2381740.0, 32129324.0],
[5.0, u'AJ', u'Azerbaijan', 86600.0, 7868385.0]]
To eliminate duplicate records. The attributes list has now 251 records. However, country names (index 2) are not sorted (printed 11 first elements).
attr2=[]
for attribute in attr:
if attribute not in attr2:
attr2.append(attribute)
print len(attr2)
251
print attr2[0:10]
[[1.0, u'AA', u'Aruba', 193.0, 71218.0],
[2.0, u'AC', u'Antigua and Barbuda', 443.0, 68320.0],
[4.0, u'AG', u'Algeria', 2381740.0, 32129324.0],
[5.0, u'AJ', u'Azerbaijan', 86600.0, 7868385.0],
[6.0, u'AL', u'Albania', 28748.0, 3544808.0],
[7.0, u'AM', u'Armenia', 29800.0, 2991360.0],
[9.0, u'AO', u'Angola', 1246700.0, 10978552.0],
[10.0, u'AQ', u'American Samoa', 199.0, 57902.0],
[11.0, u'AR', u'Argentina', 2766890.0, 39144753.0],
[12.0, u'AS', u'Australia', 7686850.0, 19913144.0]]
To sort by field (index 2) is necessary to use one anonymous function with lambda in 'sort'. Now, country names are sorted.
attr2.sort(key=lambda x: x[2])
print attr2[0:10]
[[3.0, u'AF', u'Afghanistan', 647500.0, 28513677.0],
[6.0, u'AL', u'Albania', 28748.0, 3544808.0],
[4.0, u'AG', u'Algeria', 2381740.0, 32129324.0],
[10.0, u'AQ', u'American Samoa', 199.0, 57902.0],
[8.0, u'AN', u'Andorra', 468.0, 69865.0],
[9.0, u'AO', u'Angola', 1246700.0, 10978552.0],
[14.0, u'AV', u'Anguilla', 102.0, 13008.0],
[15.0, u'AY', u'Antarctica', 1.0, 1.0],
[2.0, u'AC', u'Antigua and Barbuda', 443.0, 68320.0],
[11.0, u'AR', u'Argentina', 2766890.0, 39144753.0]]
If you want to sort, for example, by 'AREA' (index 3):
attr_id = wb.pendingAllAttributesList()
for i in attr_id: #print index and field names
print i,wb.attributeDisplayName(i)
0 CAT
1 FIPS_CNTRY
2 CNTRY_NAME
3 AREA
4 POP_CNTRY
attr2.sort(key=lambda x: x[3])