Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort Question for all

Status
Not open for further replies.

alsjka

Technical User
Jan 21, 2003
114
0
0
US
Hi i have output like the following

FI-7-M
FI-7-M
FI-7-M
FI-7-S
FI-7-C
FI-7-CAB
FI-7-A
FI-7-B
FI-26-M
FI-10-M
FI-1-A

AND SINCE NORMAL SORTING PRODUCES BAD RESULTS
FI-26-M
FI-7-A
AND SO ON (SIMPLY INSTEAD OF SORTING 1,2,3 I GET 1,10,2,20)
HOW DO YOU GO ABOUT SORTING SOMETHING LIKE I HAVE ABOVE IN A NORMAL ORDER EITHER DESC OR ASC?

 
Zero padding would'be been a good thing. Zero padding is your friend.
Code:
FI-01-A
FI-07-A
FI-07-B
FI-07-C
FI-07-CAB
FI-07-M
FI-07-M
FI-07-M
FI-07-S
FI-10-M
FI-26-M
would sort correctly.

That being said, if you're unable to do any zero padding (like, if these are part numbers from another legacy system, say), you're going to have to do something wacky with some kind of hash or structure.

The structure option might look something like:
Code:
<CFSET lstMyValues = &quot;FI-7-M,FI-7-M,FI-7-M,FI-7-S,FI-7-C,FI-7-CAB,FI-7-A,FI-7-B,FI-26-M,FI-10-M,FI-1-A&quot;>

<CFSET strValueStruct = StructNew()>
<CFLOOP list=&quot;#lstMyValues#&quot; index=&quot;whichValue&quot; delimiters=&quot;,&quot;>
    <CFSET sKey = ListGetAt(&quot;#whichValue#&quot;,2,&quot;-&quot;)>
    <CFIF NOT StructKeyExists(strValueStruct,sKey)>
        <CFSET strValueStruct[sKey] = ArrayNew(1)>
    </CFIF>
    <CFSET ArrayAppend(strValueStruct[sKey],whichValue)>
</CFLOOP>
<CFLOOP list=&quot;#ListSort(StructKeyList(strValueStruct),&quot;numeric&quot;)#&quot; index=&quot;whichArray&quot;>
    <CFSET lstSortedValues = ListSort(&quot;#ArrayToList(strValueStruct[whichArray])#&quot;,&quot;text&quot;)>
    <CFLOOP list=&quot;#lstSortedValues#&quot; index=&quot;whichSortedValue&quot;>
        <CFOUTPUT>#whichSortedValue#<br /></CFOUTPUT>
    </CFLOOP>
</CFLOOP>

Yuck!

Or, perhaps a more elegant solution would be to use a query.
Code:
<CFSET qryValueSet = QueryNew(&quot;section1,section2,section3&quot;)>
<CFLOOP list=&quot;#lstMyValues#&quot; index=&quot;whichValue&quot; delimiters=&quot;,&quot;>
	<CFSCRIPT>
	    QueryAddRow(qryValueSet);
		QuerySetCell(qryValueSet,&quot;section1&quot;,ListGetAt(&quot;#whichValue#&quot;,1,&quot;-&quot;));
		QuerySetCell(qryValueSet,&quot;section2&quot;,ListGetAt(&quot;#whichValue#&quot;,2,&quot;-&quot;));
		QuerySetCell(qryValueSet,&quot;section3&quot;,ListGetAt(&quot;#whichValue#&quot;,3,&quot;-&quot;));
	</CFSCRIPT>
</CFLOOP>

<CFQUERY name=&quot;qrySortedValues&quot; dbtype=&quot;query&quot;>
	SELECT *
	FROM qryValueSet
	ORDER BY section1,section2,section3
</CFQUERY>

<p>
<CFOUTPUT query=&quot;qrySortedValues&quot;>
#qrySortedValues.section1#-#qrySortedValues.section2#-#qrySortedValues.section3#<br />
</CFOUTPUT>
</p>

If you're getting the list of values from a query already, you might be able to set up alias columns in that query as you're selecting it... which means you wouldn't have to do it in CFML:
Code:
<CFQUERY ...>
   SELECT *, SUBSTRING(part_number,1,CHARINDEX(&quot;-&quot;,part_number)) AS section1, ...
      :
</CFQUERY>
depending on your database.


-Carl
 
What if you wrote a replace function to take out the dashes and replace them with empty space and then sorted it?
 
You'd still get the same sort order.
If you stripped out the dashes, and replaced them with spaces, you'd now have values of:
Code:
FI 7 M
FI 7 M
FI 7 M
FI 7 S
FI 7 C
FI 7 CAB
FI 7 A
FI 7 B
FI 26 M
FI 10 M
FI 1 A
which would still have the undesired effect of sorting as:
Code:
FI 1 A
FI 10 M
FI 26 M
FI 7 A
FI 7 B
FI 7 C
FI 7 CAB
FI 7 M
FI 7 M
FI 7 M
FI 7 S


-Carl
 
OK this is the code i Have csteinhilber. I seem to be have trouble understanding what you are trying to explain. Can you clarify more...

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Items Not Physical Inventoried</title>
</head>
<link rel=&quot;stylesheet&quot; href=&quot;/stylesheets/crayola.css&quot; type=&quot;text/css&quot;>


<body>
<cfquery name=&quot;get_verify2&quot; datasource=&quot;mp2sql&quot;>
select warehouseinfo.itemnum, warehouseinfo.LASTDATECOUNTED,warehouseinfo.warehouseid, warehouseinfo.STOCKITEM,stock.qtyonhand, stock.location
from warehouseinfo, stock
where warehouseinfo.itemnum = stock.itemnum
and warehouseinfo.WAREHOUSEID = '#cfs_warehouse#'
and stock.warehouseid = '#cfs_warehouse#'
and warehouseinfo.lastdatecounted < '#form.txt_date#'
and warehouseinfo.itemnum in (select itemnum from invy where type not like '%fas%')
and warehouseinfo.itemnum in (select itemnum from stock where location not like 'FI-GAR%')
and warehouseinfo.itemnum in (select itemnum from invy where itemnum not like 'ELEC-WIRE%')
and warehouseinfo.itemnum in (select itemnum from invy where ud2 not like 'mexico')
and warehouseinfo.stockitem = 'y'
group by warehouseinfo.itemnum, warehouseinfo.LASTDATECOUNTED,warehouseinfo.warehouseid, warehouseinfo.STOCKITEM,stock.qtyonhand, stock.location
ORDER BY case when isnumeric(location) <> 0 then convert(int,location)end DESC
</cfquery>

<p align=&quot;center&quot;>
<cfoutput><font>#cfs_warehouse#</font></cfoutput><br>
<font><strong>Total Uncounted Items - </font><font color=&quot;#ff0000&quot;><cfoutput>#get_verify2.recordcount#</cfoutput></strong></font>
</p>
<table align=&quot;center&quot; bordercolor=&quot;Silver&quot; border=&quot;1&quot; cellpadding=&quot;3&quot;>
<tr bgcolor=&quot;#BFDFFF&quot; bordercolor=&quot;Blue&quot;>
<td align=&quot;center&quot;>
<font ><strong>Part Number</strong></font>
</td>
<td align=&quot;center&quot;>
<font ><strong>Last Date Counted</strong></font>
</td>
<td align=&quot;center&quot;>
<font ><strong>Stock Location</strong></font>
</td>
<td align=&quot;center&quot;>
<font ><strong>Qty On Hand</strong></font>
</td>
</tr>
<cfloop query = &quot;get_verify2&quot;>

<tr>
<td><cfoutput><font>#get_verify2.itemnum#</font></cfoutput></td>
<td><cfoutput><font>#dateformat(get_verify2.LASTDATECOUNTED)#</font></cfoutput></td>

<td><cfoutput><font>#get_verify2.location#</font></cfoutput></td>
<td><cfoutput><font>#get_verify2.qtyonhand#</font></cfoutput></td>
</tr>
</cfloop>
</table>
<cfinclude TEMPLATE=&quot;/mp2/include/back_arrow_mp2.cfm&quot;>

<cfoutput></cfoutput>
</body>
</html>
 
Okay... so you are getting the values from a resultset already. So, you have a couple of options... the last solution I gave may work, depending on which database you're using.

Basically what you need to do is break the
Code:
itemnum
into three separate sections (based on your original example), and sort on each section in order... so you sort on the first section (&quot;FI&quot;) first, the second section (&quot;7&quot;, &quot;26&quot;, &quot;10&quot;, etc) second, and so on.

If, as I said, your database will support the proper SQL extensions, the query solution would probably be the fastest. Where you can break that itemnum into the appropriate sections right there in the SQL as you're querying the table, and alias the sections as new column names:
Code:
<CFQUERY name=&quot;get_verify2&quot; datasource=&quot;mp2sql&quot;>
   SELECT warehouseinfo.itemnum,
          warehouseinfo.LASTDATECOUNTED,
          warehouseinfo.warehouseid, 
          warehouseinfo.STOCKITEM,
          stock.qtyonhand,
          stock.location,
Code:
          SUBSTRING(
            warehouseinfo.itemnum,
            1,
            (
              CHARINDEX(&quot;-&quot;,warehouseinfo.itemnum,1) - 1
            )
          ) AS itemnum_section1,

          SUBSTRING(
            warehouseinfo.itemnum,
            (
              CHARINDEX(&quot;-&quot;,warehouseinfo.itemnum,1) + 1
            ),
            (
              CHARINDEX(&quot;-&quot;,warehouseinfo.itemnum,(CHARINDEX(&quot;-&quot;,warehouseinfo.itemnum,1) + 1)) - 1
            )

         ) AS itemnum_section2,

          SUBSTRING(
            warehouseinfo.itemnum,
            (
              CHARINDEX(&quot;-&quot;,warehouseinfo.itemnum,(CHARINDEX(&quot;-&quot;,warehouseinfo.itemnum,1) + 1)) + 1
            ),
            (
              LEN(warehouseinfo.itemnum)
            )

         ) AS itemnum_section3,
Code:
   FROM warehouseinfo, stock
         :
         :
   ORDER BY itemnum_section1,itemnum_section2,itemnum_section3

What all that SUBSTRING business does (or should do... I haven't tested it) is pull out each section of your itemnum and assign the contents to a new column name (&quot;itemnum_section1&quot;, &quot;itemnum_section2&quot; and &quot;itemnum_section3&quot;)... then you order the results by those columns.

So your items should be sorted properly right out of the starting gate, and your CFLOOP should work as you desire.


If your database doesn't support SUBSTRING and/or CHARINDEX, you may need to do a little research to determine if there are alternatives for those functions for your database (MID and POS could be possibilities). Or, if there simply aren't functions that perform those functions, you'll have to use one of the other solutions I mentioned (the structure or the manual query).





-Carl
 
This is the error i get?

Error Executing Database Query.[Macromedia][SQLServer JDBC Driver][SQLServer]Invalid column name '-'. The specific sequence of files included or processed is:

Any comments?




<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Items Not Physical Inventoried</title>
</head>
<link rel=&quot;stylesheet&quot; href=&quot;/stylesheets/crayola.css&quot; type=&quot;text/css&quot;>


<body>
<cfquery name=&quot;get_verify2&quot; datasource=&quot;mp2sql&quot;>
select warehouseinfo.itemnum, warehouseinfo.LASTDATECOUNTED,warehouseinfo.warehouseid, warehouseinfo.STOCKITEM,stock.qtyonhand, stock.location,
SUBSTRING(stock.location,
1,
(
CHARINDEX(&quot;-&quot;,stock.location,1) - 1
)
) AS itemnum_section1,

SUBSTRING(
stock.location,
(
CHARINDEX(&quot;-&quot;,stock.location,1) + 1
),
(
CHARINDEX(&quot;-&quot;,stock.location,(CHARINDEX(&quot;-&quot;,stock.location,1) + 1)) - 1
)

) AS itemnum_section2,

SUBSTRING(
stock.location,
(
CHARINDEX(&quot;-&quot;,stock.location,(CHARINDEX(&quot;-&quot;,stock.location,1) + 1)) + 1
),
(
LEN(stock.location)
)

) AS itemnum_section3

FROM warehouseinfo, stock
where warehouseinfo.itemnum = stock.itemnum
and warehouseinfo.WAREHOUSEID = '#cfs_warehouse#'
and stock.warehouseid = '#cfs_warehouse#'
and warehouseinfo.lastdatecounted < '#form.txt_date#'
and warehouseinfo.itemnum in (select itemnum from invy where type not like '%fas%')
and warehouseinfo.itemnum in (select itemnum from stock where location not like 'FI-GAR%')
and warehouseinfo.itemnum in (select itemnum from invy where itemnum not like 'ELEC-WIRE%')
and warehouseinfo.itemnum in (select itemnum from invy where itemnum not like 'PT-BUS%')
and warehouseinfo.itemnum in (select itemnum from invy where itemnum not like 'ELEC-STAKON%')

and warehouseinfo.itemnum in (select itemnum from invy where ud2 not like 'mexico')
and warehouseinfo.stockitem = 'y'
group by warehouseinfo.itemnum, warehouseinfo.LASTDATECOUNTED,warehouseinfo.warehouseid, warehouseinfo.STOCKITEM,stock.qtyonhand, stock.location
ORDER BY itemnum_section1,itemnum_section2,itemnum_section3

</cfquery>

<p align=&quot;center&quot;>
<cfoutput><font>#cfs_warehouse#</font></cfoutput><br>
<font><strong>Total Uncounted Items - </font><font color=&quot;#ff0000&quot;><cfoutput>#get_verify2.recordcount#</cfoutput></strong></font>
</p>
<table align=&quot;center&quot; bordercolor=&quot;Silver&quot; border=&quot;1&quot; cellpadding=&quot;3&quot;>
<tr bgcolor=&quot;#BFDFFF&quot; bordercolor=&quot;Blue&quot;>
<td align=&quot;center&quot;>
<font ><strong>Part Number</strong></font>
</td>
<td align=&quot;center&quot;>
<font ><strong>Last Date Counted</strong></font>
</td>
<td align=&quot;center&quot;>
<font ><strong>Stock Location</strong></font>
</td>
<td align=&quot;center&quot;>
<font ><strong>Qty On Hand</strong></font>
</td>
</tr>
<cfloop query = &quot;get_verify2&quot;>

<tr>
<td><cfoutput><font>#get_verify2.itemnum#</font></cfoutput></td>
<td><cfoutput><font>#dateformat(get_verify2.LASTDATECOUNTED)#</font></cfoutput></td>
<!--- <cfquery name=&quot;get_verify4&quot; datasource=&quot;mp2sql&quot;>
select location,itemnum,qtyonhand
from stock
where itemnum = '#get_verify2.itemnum#'
and WAREHOUSEID = '#cfs_warehouse#'
and itemnum in (select itemnum from stock where location not like 'FI-GAR%')
</cfquery> --->
<td><cfoutput><font>#get_verify2.location#</font></cfoutput></td>
<td><cfoutput><font>#get_verify2.qtyonhand#</font></cfoutput></td>
</tr>
</cfloop>
</table>
<cfinclude TEMPLATE=&quot;/mp2/include/back_arrow_mp2.cfm&quot;>

</body>
</html>
 
Try using single quotes in the CHARINDEX functions, rather than the double quotes. Sorry about that.

ex:
Code:
  CHARINDEX('-',stock.location,1)
etc



-Carl
 
newest error any response?

Error Executing Database Query.[Macromedia][SQLServer JDBC Driver][SQLServer]Invalid length parameter passed to the substring function.
 
ok got it had to remove the &quot;-1&quot;

SUBSTRING(stock.location,
1,
(
CHARINDEX(&quot;-&quot;,stock.location,1)
)
) AS itemnum_section1,
 
Thanks again for all your help and support..

 
I assume adding a Column to the Table in the database was out of the question. If not why didn't you just add a column called &quot;sort&quot; and give each record an number based on how it should be sorted.
My 2 cents
 
Or better yet give it 3 columns one for FI a second one for the second number and a 3rd for the 3rd number then sort by those columns.
 
That's essentially what we're doing with the SQL above... splitting out the part number into three different columns (via aliasing).

I kind of assumed that it was a part number coming from a legacy database, and that the actual table couldn't be altered. My bias, I suppose ( but based on lots of experience working with legacy data ) ;-)



-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top