diff --git a/indra/newview/llfloatermarketplacelistings.cpp b/indra/newview/llfloatermarketplacelistings.cpp
index 10c6045f875ce835ec69420eac30a2b2e515f5c2..9bbe63de72aa61170c712780fb145a65aafa8255 100755
--- a/indra/newview/llfloatermarketplacelistings.cpp
+++ b/indra/newview/llfloatermarketplacelistings.cpp
@@ -95,7 +95,7 @@ void LLPanelMarketplaceListings::onViewSortMenuItemClicked(const LLSD& userdata)
 	if (chosen_item == "sort_by_stock_amount")
 	{
         mSortOrder = (mSortOrder == LLInventoryFilter::SO_FOLDERS_BY_NAME ? LLInventoryFilter::SO_FOLDERS_BY_WEIGHT : LLInventoryFilter::SO_FOLDERS_BY_NAME);
-        mAllPanel->getFolderViewModel()->setSorter(mSortOrder);
+        mAllPanel->setSortOrder(mSortOrder);
 	}
     // View/filter options
 	else if (chosen_item == "show_all")
diff --git a/indra/newview/llfolderviewmodelinventory.cpp b/indra/newview/llfolderviewmodelinventory.cpp
index aac3a41b9eedf1849a76f052b7d8832100138ca2..c31b40b17994ae82bb220704169824d7a35ebcb2 100755
--- a/indra/newview/llfolderviewmodelinventory.cpp
+++ b/indra/newview/llfolderviewmodelinventory.cpp
@@ -27,6 +27,7 @@
 #include "llviewerprecompiledheaders.h"
 #include "llfolderviewmodelinventory.h"
 #include "llinventorymodelbackgroundfetch.h"
+#include "llinventoryfunctions.h"
 #include "llinventorypanel.h"
 #include "lltooldraganddrop.h"
 #include "llfavoritesbar.h"
@@ -269,7 +270,7 @@ bool LLInventorySort::operator()(const LLFolderViewModelItemInventory* const& a,
 
 	// We sort by name if we aren't sorting by date
 	// OR if these are folders and we are sorting folders by name.
-	bool by_name = (!mByDate || (mFoldersByName && (a->getSortGroup() != SG_ITEM)));
+	bool by_name = ((!mByDate || (mFoldersByName && (a->getSortGroup() != SG_ITEM))) && !mFoldersByWeight);
 
 	if (a->getSortGroup() != b->getSortGroup())
 	{
@@ -301,6 +302,35 @@ bool LLInventorySort::operator()(const LLFolderViewModelItemInventory* const& a,
 			return (compare < 0);
 		}
 	}
+    else if (mFoldersByWeight)
+    {
+        S32 weight_a = compute_stock_count(a->getUUID());
+        S32 weight_b = compute_stock_count(b->getUUID());
+        if ((weight_a != -1) || (weight_b != -1))
+        {
+            llinfos << "Merov : sort by weight, a = " << a->getName() << ", " << weight_a << ", b = " << b->getName() << ", " << weight_b << llendl;
+        }
+		if (weight_a == weight_b)
+		{
+            // Equal weight -> use alphabetical order
+			return (LLStringUtil::compareDict(a->getDisplayName(), b->getDisplayName()) < 0);
+		}
+		else if (weight_a == -1)
+        {
+            // No weight -> move a at the end of the list
+            return false;
+        }
+        else if (weight_b == -1)
+        {
+            // No weight -> move b at the end of the list
+            return true;
+        }
+        else
+		{
+            // Lighter is first (sorted in increasing order of weight)
+            return (weight_a < weight_b);
+        }
+    }
 	else
 	{
 		time_t first_create = a->getCreationDate();
diff --git a/indra/newview/llfolderviewmodelinventory.h b/indra/newview/llfolderviewmodelinventory.h
index 9dcfdfa185f83f12c548ffaee3c2d7adcecdb2ce..b6d2c8502bce4fbc0a6916ac4bcbbb697bef570f 100755
--- a/indra/newview/llfolderviewmodelinventory.h
+++ b/indra/newview/llfolderviewmodelinventory.h
@@ -89,6 +89,7 @@ class LLInventorySort
 		mByDate = (mSortOrder & LLInventoryFilter::SO_DATE);
 		mSystemToTop = (mSortOrder & LLInventoryFilter::SO_SYSTEM_FOLDERS_TO_TOP);
 		mFoldersByName = (mSortOrder & LLInventoryFilter::SO_FOLDERS_BY_NAME);
+		mFoldersByWeight = (mSortOrder & LLInventoryFilter::SO_FOLDERS_BY_WEIGHT);
 	}
 
 	bool operator()(const LLFolderViewModelItemInventory* const& a, const LLFolderViewModelItemInventory* const& b) const;
@@ -97,6 +98,7 @@ class LLInventorySort
 	bool mByDate;
 	bool mSystemToTop;
 	bool mFoldersByName;
+	bool mFoldersByWeight;
 };
 
 class LLFolderViewModelInventory
diff --git a/indra/newview/llinventoryfunctions.cpp b/indra/newview/llinventoryfunctions.cpp
index ff38017d6fcd1d3f5d22619ccd10bfda21b83546..57e1b6d9bccff6d4e42e20d6434c48c0bc1b9e7a 100755
--- a/indra/newview/llinventoryfunctions.cpp
+++ b/indra/newview/llinventoryfunctions.cpp
@@ -796,6 +796,11 @@ S32 compute_stock_count(LLUUID cat_uuid)
 {
     // Handle the case of the folder being a stock folder immediately
     LLViewerInventoryCategory* cat = gInventory.getCategory(cat_uuid);
+    if (!cat)
+    {
+        // Not a category so no stock count to speak of
+        return -1;
+    }
     if (cat->getPreferredType() == LLFolderType::FT_MARKETPLACE_STOCK)
     {
         // Note: stock folders are *not* supposed to have nested subfolders so we stop recursion here