I have a shapefile with only 4 rows in it. I already could filter it so I have only 2 left. I need these sorted by the 'length' field, then take the first and second row to save in to individual files. Here is the code for sorting:

import arcpy
fc = 'C:\\Users\\test.shp'
fields = ['length', 'FID']
expression = arcpy.AddFieldDelimiters(fc, "length") + " > 1000"
for row in sorted(arcpy.da.SearchCursor(fc, fields,)):
    print('{0}, {1}'.format(row[0], row[1]))

For saving in to the new file:

arcpy.FeatureClassToFeatureClass_conversion(?, outLocation, 

How can I make the search cursor to show first only the lower value row, then the higher one? (order doesn't matter)

How can I select the number 1 row, then the number 2 row?

  • When you say "lower value row" do you mean the value in one of the fields? Commented Dec 15, 2015 at 21:15
  • Yes, in the length field
    – Gary
    Commented Dec 15, 2015 at 21:24
  • given the fact you're only working with four records, you could just create a list containing the values of the fields you require, sort the list, and then create an SQL query which selects the records which have the values in the first two indexes of the list. This wouldn't be efficient if you ended up working with a large number of records of course.
    – Adam
    Commented Dec 15, 2015 at 22:27
  • @Adam After shorting who could I select the first or second record? I only have 4 records in the file but have more than 80 shapefiles
    – Gary
    Commented Dec 16, 2015 at 0:43
  • 1
    I won't write the code for you, but the basic idea would be: 1) iterate over the records to get the values of the field required, and store them in a list. 2) sort the list in ascending order. 3) get the values from the [0] and [1] indexes of the list. 4) use these values in a SQL query to select records which equal them, for example SELECT * FROM MY_DATA WHERE "MY_FIELD" IN (myList[0], myList[1])
    – Adam
    Commented Dec 16, 2015 at 1:22

3 Answers 3


I'd have an end goal of creating an sql query with the OIDs of the two features.

Create a sorted list of tuples of lengths and oids from your features. Pull out the first two oids from your sorted list and create an sql from the oids. You can then apply the sql when you copy your features.

import arcpy
fc = 'C:\\Users\\test.shp'
expression = arcpy.AddFieldDelimiters(fc, "length") + " > 1000"

#sorted list of tuples of length and oid
lis = sorted ([(length, oid) for length, oid in
                (fc, ["length", "OID@"],

#get top two oids
twoOids = [str (oid) for length, oid in lis] [:2]

#sql expression
oidStr = ", ".join (twoOids)
oidFld = arcpy.Describe (fc).OIDFieldName
delimOidFld = arcpy.AddFieldDelimiters (fc, oidFld)
sql = "{0} IN ({1})".format (delimOidFld, oidStr)

#export with sql
arcpy.FeatureClassToFeatureClass_conversion(fc, outLocation, outFeatureClass, sql)

i think you are asking about how to select just the first and second records of a search cursor.

i was looking into this same activity recently and came across the several options, all using variations of "next" function, depending on the cursor type (arcpy.SearchCursor vs. arcpy.da.SearchCursor).

Perhaps more saavy ArcPy and Python folks will be able to explain more.



    for row in cursorA: 
        #row = next(cursorA)   # get first record option 1
        #row = cursorA.next()   # get first record option 2
        next(cursorA)   # get first record option 3
  • assuming that the cursor is running through the records in the order required
    – Adam
    Commented Dec 17, 2015 at 3:24

You do not state what your license level is; this is always important as it dictates the solution. Having said that, consider using the Sort tool (which requires an Advanced license). More information can be found here.

  • Thx, but this doesn't really help. I don't have problem with the sorting, I have problem with selecting the row.
    – Gary
    Commented Dec 15, 2015 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.